2016-08-05 40 views
-4

告訴我查詢是否正確,或包含任何錯誤:如何確定SQL語句是否會運行?

select cname from company where id IN (
    select company_id,count(*) from medication 
    group by(company_id) having count(*)>1 
) 
order by cname; 
+1

它是否運行?你有沒有得到任何錯誤? – tadman

+1

取決於表結構和預期的結果,查詢將產生 –

回答

0

包含你不能在(選擇COMPANY_ID,計數(錯誤刪除計數()可能適合您,請檢查以下

/* 
create table company(id int,cname varchar(10)); 
insert into company values 
(1,'abc'),(2,'def'),(3,'ghi'); 

create table medication(id int,company_id int); 
truncate table medication; 
insert into medication values 
(1,1), 
(2,1), 
(3,1), 
(4,2), 
(5,2), 
(6,2), 
(7,3), 
(8,2); 
*/ 

這個代碼

select cname from company 
where id IN (select company_id from medication group by(company_id) having count(*) > 1) 
order by cname; 

結果

+-------+ 
| cname | 
+-------+ 
| abc | 
| def | 
+-------+