简体   繁体   English

使用和的嵌套sql语句

[英]nested sql statement using and

how to make this work in mysql? 如何使它在mysql中工作?

select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where (city in ( select suburb from allsuburbs)) and CATEGORY='Banks' 选择来自维多利亚州的ID,COM​​PANY_NAME,contact1,SUBURB,CATEGORY,PHONE(城市(从所有郊区中选择郊区)),CATEGORY =“银行”

this below statement is working: select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where city in ( select suburb from allsuburbs) 下面的语句有效:从城市所在的维多利亚州选择ID,COM​​PANY_NAME,contact1,SUBURB,CATEGORY,PHONE(从所有郊区中选择郊区)

if I add "and" , it gives me an empty resultset, thanks 如果我加上“和”,它给我一个空的结果集,谢谢

Learn how joins work. 了解联接的工作方式。

select 
  v.ID,v.COMPANY_NAME,v.contact1,v.SUBURB,v.CATEGORY,v.PHONE 
from
  Victoria v
  inner join allsuburbs s on s.suburb = v.city 
where 
  v.CATEGORY='Banks'

Apart from that, your query does not make a whole lot of sense. 除此之外,您的查询没有任何意义。

  • Your table is namend Victoria , but it contains a field named city ?! 您的表名为Victoria ,但其中包含一个名为city ?!的字段。 Do your other cities have their own table too? 您的其他城市也有自己的桌子吗?
  • You have a table named allsuburbs , but your criterion is that Victoria.city equals allsuburbs.suburb , even though a field named Victoria.suburb exists?! 您有一个名为allsuburbs的表,但您的条件是即使存在一个名为Victoria.suburb的字段, Victoria.city等于allsuburbs.suburb吗? What's Victoria.suburb for, then? 那么, Victoria.suburb是什么?
  • Your table is named allsuburbs . 您的表名为allsuburbs Do you have another table that contains suburbs or is this your only one? 您是否还有另一张包含郊区的表格,或者这是您唯一的一张吗? If it is your only one, the name is redundant. 如果只有您一个,那么这个名称就多余了。
  • You have a field contact1 . 您有一个字段contact1 Do you have contact2 ... contact10 as well? 您是否还有contact2 ... contact10 Bad database design. 错误的数据库设计。
  • Why is half of your fieldnames in caps, and not all of them (or none of them)? 为什么您的半数域名全都用大写而不是大写(或全都不用)?

Oh, and the usual format for SQL is: SQL keywords in caps, the field names etc. in mixed case/lower case. 哦,SQL的常用格式是:大写的SQL关键字,大小写混合的字段名等。 Much easier to read. 更容易阅读。

I think you might have misplaced a parentheses? 我认为您可能放错了括号?

.. PHONE from Victoria where 
(city in ( select suburb from allsuburbs)) and CATEGORY='Banks'

I'm guessing should be: 我猜应该是:

.. PHONE from Victoria where 
city in ( select suburb from allsuburbs) and CATEGORY='Banks'

Not sure if that makes more sense, but the first case is not an ok SQL-statement I believe. 不知道这是否更有意义,但是我认为第一种情况不是好的SQL语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM