關於sql查詢,想從很多表中查詢欄位值

2021-12-23 01:38:35 字數 5762 閱讀 1937

1樓:匿名使用者

select  q.條碼 ,

(case when  isnull(a.a站點,'')>'' then 't' else 'f' end )   '是否經過a站點',

(case when  isnull(b.b站點,'')>'' then 't' else 'f' end  ) '是否經過b站點'

from 條碼錶  q

left join a a  on a.條碼=q.條碼left join b b on b.條碼 =q.條碼這是舉的一個簡單的例子

2樓:小李無刀

select 條碼,日期,站1名 from 站1的表 where 欄位=條碼

union all

select 條碼,日期,站2名 from 站2的表 where 欄位=條碼

union all

select 條碼,日期,站3名 from 站3的表 where 欄位=條碼

.。。。。。。。。所有的站的表都用union all連起來,就可以一起查出來到一個**裡

3樓:

那何必這樣做,多加個表不就完了,把這些都記錄到這張表裡

sql如何根據一個欄位的多個值查詢

4樓:tutu天然呆

具體方法如下:

假定表名test,列id是數值型別。

用同一個欄位的多個值作為條件來查詢可以使用in或者or。

具體語句如下:

1、select * from test where id in (1,2,3)

2、select * from test where id = 1 or id =2 or id = 3

顯然第一種方法更簡便。

ps: 如果如你訊息所說,有一個選課表test,學生號id,所選課程名name,那麼,檢索同時選擇了美術、體育、**三門課程的學生id的語法如下:

select a.id from test a,test b,test c

where a.id = b.id and b.i

d = c.id and a.name = '美術' and b.name = '體育' and c.name = '**';

問題的關鍵,在於test表通過別名做三次關聯查詢。

5樓:匿名使用者

select 別名.欄位1,別名.欄位2,別名.欄位3 from 表.別名 where 別名.欄位1 in ('欄位1值1',欄位1值2'','欄位1值3');

用關鍵字 in實現 一個欄位的多個值查詢,上面使用偽**舉例

希望我的回答對你有幫助。。

6樓:匿名使用者

如果menuid是列表1,5,8

那麼select distinct companyid from menutable where menuid in('1','5','8')(如果menuid為字元型別,數字型別將引號去掉)

如果傳入的menuid是個字串1,5,8那麼寫成select distinct companyid from menutable where ',1,5,8,' like '%,'+cast(menuid as varchar)+',%'

7樓:匿名使用者

select companyid from 表名 where menuid in (值 , 值 , ……)

或者 menuid是根據別的條件從別的地方查出來的

select companyid from 表名 where menuid in (select menuid from .....)

8樓:匿名使用者

select menuname from 表名

where menuid='' //查詢條件

sql語句查詢同一欄位滿足多個值如何寫呢?

9樓:匿名使用者

查詢得到的資料需要如下老公 lg年齡 lg月薪 老婆 lp年齡 lp月薪 合計年齡 合計工資張某 27 500 李某 26 800 53 1300 select 老公,a.年齡 as lg

10樓:

可以、select * from tel where phone in('0773','0775') order by num desc

11樓:明明加油吧白羊

select * from tel where phone =0773 or phone=0775 order by num desc

sql語句從一張表查詢一個欄位值插入另一個表中

12樓:小丁創業

標準sql語句格式:

insert

into 表名(欄位名)

select 欄位名

from 表面

例子:將查詢出的s表中sno,j表中jno,p表中pno插入spj表中

insert

into spj(sno,jno,pno)select sno,jno,pno

from s,j,p

13樓:古舟蓑笠翁

insert into a(name,num,class) values('小米','1001',(select class from b where name = '小米'));

14樓:2一瞬間

15樓:匿名使用者

insert into a(產品,編號, 型別)

select 產品,編號, 型別 from b

where 產品=‘小米’

sql語句中怎麼把查詢出來的欄位資料當表名再進行查詢?

16樓:愛可生雲資料庫

table 語句

具體語法:table table_name [order by column_name] [limit number [offset number]]

其實從語法上看,可以排序,也可以過濾記錄集,不過比較簡單,沒有 select 那麼強大。

示例 1

簡單的建一張很小的表 y1,記錄數為 10 條。表 t1,插入 10 條記錄

mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

query ok, 0 rows affected (0.02 sec)

mysql-(ytt/3305)->insert into t1

with recursive aa(a,b) as (

select 1,1

union all

select a+1,ceil(rand()*20) from aa where a < 10

) select * from aa;

query ok, 10 rows affected (0.00 sec)

records: 10  duplicates: 0  warnings: 0

簡單全表掃描mysql-(ytt/3305)->select * from t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

table 結果mysql-(ytt/3305)->table t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

看下 table 的執行計劃mysql-(ytt/3305)->explain table t1 order by r1 limit 2\g*************************** 1. row ***************************           id: 1  select_type:

******        table: t1   partitions: null         type:

allpossible_keys: null          key: null      key_len:

null          ref: null         rows: 10     filtered:

100.00        extra: using filesort1 row in set, 1 warning (0.

00 sec)

其實可以看到 table 內部被 mysql 轉換為 select 了。mysql-(ytt/3305)->show warnings\g*************************** 1. row ***************************  level:

note   code: 1003message: /* select#1 */ select `ytt`.

`t1`.`r1` as `r1`,`ytt`.`t1`.

`r2` as `r2` from `ytt`.`t1` order by `ytt`.`t1`.

`r1` limit 21 row in set (0.00 sec)

那其實從上面簡單的例子可以看到 table 在內部被轉成了普通的 select 來處理。示例 2應用於子查詢裡的子表。這裡要注意,內表的欄位數量必須和外表過濾的欄位數量一致。

克隆表 t1 結構mysql-(ytt/3305)->create table t2 like t1;query ok, 0 rows affected (0.02 sec)

克隆表 t1 資料mysql-(ytt/3305)->insert into t2 table t1;query ok, 10 rows affected (0.00 sec)records: 10  duplicates:

0  warnings: 0

table t1 被當做內表,表 t1 有兩個欄位,必須同時滿足 t2 檢索時過濾的欄位也是兩個。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)

注意:這裡如果過濾的欄位數量和子表數量不一致,則會報錯。

sql多表查詢

select lineid,id,country from domestic union all select lineid,id,country from freedom 聯合查詢domestic,freedom表的lineid,id,country all代表不去除重複 功能 sql語句 uni...

sql多表聯合查詢傳入的日期引數對應兩個欄位,怎麼判斷欄位為空則查詢另欄位,反之查詢這個欄位 求

你這看著那麼麻煩,舉個例子,自己套進去好了。select case when 欄位a is null then 欄位b else 欄位a end from 表名 where 連線和版where條件權正常使用。select 抄case when 欄位a is null then 欄位b else 欄位...

關於SQL的,兩表查詢

select a.編號,a.姓名,a.性別,b.狀態 as 語文,c.狀態 as 數學,d.狀態 as 英語 from 表a a,表b b,表b c,表b dwhere a.語文 b.狀態編號 anda.語文 c.狀態編號 and a.語文 d.狀態編號 補充問題答案 select a.編號,a.姓...