数据库SQL组合查询,两表之间取数据

数据库SQL组合查询,两表之间取数据,第1张

select

*

from

B

where

id

in

(select

Max(ID)

from

B

where

B.Name

in

(select

name

from

A

where

pid='99999')

group

by

name

)

可以这样

select * from 表一 where to_days(datetime) = to_days(now()) and username ='$user'

union all

select * from 表二 where to_days(datetime) = to_days(now()) and username ='$user'

如果你非要排序

select t.*

from

(select * from 表一 where to_days(datetime) = to_days(now()) and username ='$user'

union all

select * from 表二 where to_days(datetime) = to_days(now()) and username ='$user' ) as t

order by t.id desc

sql联合查询语句(两张表)是:

select A.ID,A.VALUE,A.TYPE,A.NAME,B.KEY,B.ID,B.VALUE,B.NAME 

min(VALUE),max(VALUE) from A left join B on A.ID = B.ID

where B.NAME="你输入的名字

and B.VALUE >(select min(VALUE) from B where NAME="你输入的名字")) 

and B.VALUE <(select min(VALUE) from B where NAME="你输入的名字"));

延展阅读:

A表字段stuid,stuname。

B表字段bid,stuid,score,coursename,status。

要用一条sql查出A表中所有记录的对应的stuid,max(score),coursename,status,并且status=1,sql语句要求跨数据库,不能使用rownum,top,limit等方言。

比如数据:

A

stuid stuname

11 zhangshan

22 lisi

B

bid sutid coursename scoure status

a 11 yuwen 66 1

b 11 shuxue 78 1

c 11 huaxue 95 0

最后要得到的数据是

stuid couresname scoure status

11 shuxue 78 1

22 null null null


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6735730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存