mysql主表查詢的where條件怎麼寫查詢另表的資料

時間 2021-10-14 22:25:42

1樓:扈懷煒

後面寫一個 欄位 in (select from 另一個表)

2樓:匿名使用者

第一種 exists與not exists

select * from table1 t1 where exists(select columnname from table2 t2 where t1.columnname2 = t2.columnname2)

第二種,in,not in的用法

select * from table1 where columnname in(select columnname from table2 where columnnane = 'condition')

第三種,any,all與比較運算的用法

-- 與任意一個比較返回真

select * from table1 where columnname > any(select columnname from table2 where columnnane = 'condition')

-- 與所有的值比較返回真

select * from table1 where columnname > all(select columnname from table2 where columnnane = 'condition')

mysql查出來的資料怎樣作為另外一張表的查詢條件

3樓:匿名使用者

可以使用子查詢,例如,從表1中查詢欄位1的值大於表2中欄位2最大值的資料,可以這麼寫:

select * from 表1 where 表1.欄位1 > (select max(欄位2) from 表2)

mysql中查詢兩個表同一條件的資料條數該怎麼寫sql語句

4樓:匿名使用者

不知道自

你的baia,b兩表有沒有

du關zhi聯,假dao定沒有關聯

select count(1)

from

(select id

from a

where id>5

union all

select id

from b

where id>5)

5樓:匿名使用者

select *** as a,*** as b,*** as c from tbl_a

union

select *** as a,*** as b,*** as c from tbl_b保證字

段名相同,不夠

的用字串頂版替權就ok了

6樓:匿名使用者

select * from a where id>5

union all

select * from b where id>5

7樓:曉瑩兒

select count(1)

from

(select id

from a

where id>5

union all

select id

from b

where id>5

)as `表名`

sql中引用一個表的查詢結果作為條件來查詢另一個表如何實現?

8樓:匿名使用者

1、用baisqlserver作為測試,建立學生、教師、班

du級三張zhi表。每張表都有一個id,int型別dao的專(自增長),作為每屬個表的主鍵。

2、新增測試資料,並建立班級與學生、教師與班級的中間表。insert into dbo.student(sname) values('張三'),插入多條,由於id自增長所以sid自動填充了。

類似將教師和班級也新增上測試資料。

3、建立班級教師表class_teacher,班級學生表class_student。

4、然後將1和2 放到1班,3和4放到2班。5和6 不放(可以理解為剛入學沒有分配班級)。然後將3個老師分配到3個班級insert into dbo.

class_teacher values (1,1)insert into dbo.class_teacher values (2,2)insert into dbo.class_teacher values (3,3)。

5、這樣,1班和2班各有兩名同學,3班沒有同學,有兩個同學沒有分配班級,每一個老師都分配了班級。現在要查詢所有班級學生情況。

9樓:樂樂愛知道

exists 這個裡面得要包含和外面表的關係的。

select 消費

號,sum(金額)金額 from 護理版卡權消費 twhere exists (

select distinct a.消費號 from 護理卡消費歷史 a left join 護理卡消費 d on a.消費號=d.消費號

where a.服務名稱 in (select b.服務名稱 from 服務類別 b where b.服務專案類別='面部護理')

and t.消費號=a.消費號 )

group by 金額,消費號

10樓:

exists 這個裡面得bai要包含和外du面表的關係的。zhiselect 消費號,sum(金額

dao)金額 from 護理版卡權消費 twhere exists (

select distinct a.消費號 from 護理卡消費歷史 a left join 護理卡消費 d on a.消費號=d.消費號

where a.服務名稱 in (select b.服務名稱 from 服務類別 b where b.服務專案類別='面部護理')

and t.消費號=a.消費號 )

group by 金額,消費號

你這樣試試

mysql:只用一條sql語句,如何查出一個表裡,不同條件對應的資料條數

11樓:匿名使用者

看一下這個sql

select

sum(

if((*** = 1 and age = 2),1,0)),sum(

if((*** = 1 and age = 5),1,0)),sum(

if((*** = 1 and age = 10),1,0))from a_test

這個sql現在就是得出的這個結果

mysql中sql語句查詢的同時根據條件將資料插入到另一個表的做法?

12樓:匿名使用者

insert into table_1 select name from table_2 where flag = ?;

這樣不可以嗎?

13樓:匿名使用者

case when then when then else end 用這個來實現你的邏輯,if好像不能多重巢狀吧,我感覺是不能。

mysql查詢Mybatis查詢

雲南新華電腦學校 spring boot mybatis mysql實現資料批量查詢,我們在構建web應用時,往往是需要查詢後臺資料庫的表記錄,查詢按返回結果型別 網海1書生 select from 表名 where left permission code,3 select left permis...

sql語句的where查詢語句中 select userid

這裡的 是引數佔位符,表示在執行查詢時用傳入的引數替換該內容在你的例子中,pstmt.setstring 1,userid 將應用得到的引數userid傳給第一個引數,pstmt.setstring 2,password 將應用得到的引數password傳給第二個引數 小白九久玩科技 where u...

mysql中0的and查詢

儘管看到問題年代有點久遠,但我覺得有必要糾正一下 在表結構中,username 欄位型別為varchar 即字串型,而非整數型 那麼查詢語句,用到username 時,必須寫成 username 比如 username 0 再比如 username admin 而如果寫成 username 0呢?0...