如何确定SQL语句是否会运行?

时间:2016-08-05 07:25:27

标签: mysql

告诉我查询是否正确或包含任何错误:

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

2 个答案:

答案 0 :(得分:0)

包含您不能的错误(选择company_id,count( 删除计数()可能对您有用,请在下面查看

/*
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   |
+-------+

答案 1 :(得分:-1)

供您参考。您始终可以在MySQl / PHPMyadmin上运行查询,以便它们显示您遇到的错误。

或者,您可以使用以下工具

https://www.piliapp.com/mysql-syntax-check/

http://sqlfiddle.com/

我希望它有所帮助