學習路之資料庫 sql查詢時新增一列固定值

2021-10-25 17:52:02 字數 1119 閱讀 7598

需求: 現有乙個表tab,表中有兩個字段,a和b,現在想查詢時候新增一列c,並且值全部為1

select t.*,1 as c from tab t;
上面是**網上的

需求:根據使用者id_查詢3張表,資料返回給前端顯示

下面是最後的sql語句  1011:為模擬使用者id

select i.id,i.created_at,i.address,i.cover_img,i.title,1 as from_table from 表a as i where user_id = 1011 union all select b.id,b.created_at,b.address,b.cover_img,b.title , 2 as from_table from 表b as b where user_id = 1011 union all select j.id,j.created_at,j.address,j.cover_img,j.title ,3 as from_table from 表c as j where user_id = 1011 order by created_at desc

用到上面的【查詢時候新增一列c,並且值全部為1 】

下面是mysql union說明,,有一點要補充的:如果表結構不同,把要查詢的列標出來且每個表都要有(不然報錯):

這裡的:    i.id,i.created_at,i.address,i.cover_img,i.title

mysql union 操作符用於連線兩個以上的 select 語句的結果組合到乙個結果集合中。多個 select 語句會刪除重複的資料。

mysql union 操作符語法格式:

select expression1, expression2, ... expression_n

from tables

[where conditions]

union [all | distinct]

select expression1, expression2, ... expression_n

from tables

[where conditions];

資料庫SQL實戰之連線查詢

查詢各個部門當前 to date 9999 01 01 領導當前薪水詳情以及其對應部門編號dept no 利用左連線實現 select s.d.dept no from salaries s left join dept manager d on s.emp no d.emp no where d....

資料庫之篩選查詢學習

number 數字型別 包含整數和小數 12 13,11.2 varchar2 字串型別 包含漢字,字母和符號,需要用單引號包裹 abc a e date 時間型別 number 5 表示是數字型別,長度是5位 number 5,2 長度為5位,最多2位小數 123.45 123.4 123.40 ...

資料庫跨庫查詢SQL

資料庫跨庫查詢sql select from opendatasource sqloledb data source user id password jshh.dbo.rs employee where code 0110 資料庫跨庫查詢功能開啟sp configure sp configure ...