SQL选择涉及多个表

时间:2018-08-14 03:23:41

标签: sql syntax

在我的SQL数据库中,我有一个带有ID列的CLIENTS表。 我有一个包含ID,CLIENTID(这是CLIENTS表中的ID)和STATUS的APPOINTMENTS表。

是否有一种方法可以编写一条SELECT语句,该语句将查找最经常使用STATUS ='NO SHOW'的客户端的客户端ID?有没有一种方法可以显示所有客户端并按STATUS ='NO SHOW'的约会存在的频率?

4 个答案:

答案 0 :(得分:2)

从TomC的答案中删除不必要的联接:

select * from (
    select CLIENTID, count(*) as NoShow 
    from APPOINTMENTS 
    WHERE STATUS='NO SHOW'
    group by CLIENTID
) q order by NoShow desc

答案 1 :(得分:0)

select C.ID,A.STATUS from CLIENTS  C
INNER JOIN APPOINEMENTS A ON A.CLIENTID = C.ID
WHERE STATUS='NO SHOW'

您的意思是这样吗?希望对您有帮助

答案 2 :(得分:0)

需要计算约会次数,并按该列表排序。除此之外,您还可以做自己想做的事。

select * from (
    select C.ID, count(*) as NoShow 
    from CLIENTS  C
    INNER JOIN APPOINEMENTS A ON A.CLIENTID = C.ID
    WHERE STATUS='NO SHOW'
    group by c.ID
) q order by NoShow desc

答案 3 :(得分:0)

如果您想要“最常出现”,则希望将结果限制为一行。 ANSI / ISO SQL构造为input { http { host => "0.0.0.0" port => 9600 codec => json } } filter { mutate { convert => ["my_field", "integer"]} } output { elasticsearch { hosts => ["elasticsearch:9200"] } stdout { codec => rubydebug } }

fetch first 1 row only

某些数据库使用select CLIENTID from APPOINTMENTS where STATUS = 'NO SHOW' group by CLIENTID order by count(*) desc fetch first 1 row only; limit代替select top (1)