MYSQL选择条件是否与Column1和Column2中的ANYTHING匹配

时间:2018-05-21 23:38:18

标签: mysql

如果我的编码在Categroy和Catecode中匹配

,我如何获得城市
"id"    "City"      "category"  "CatCode" "country"
"1"     "NewYork"   "0"         "101"     "US"
"2"     "Alaska"    "1"         "102"     "US"
"3"     "NewOrln"   "3"         "101"     "US"
"4"     "MashSh"    "4"         "102"     "US"

拜托,您可以帮忙查询吗? 谢谢。

我需要结果 例 如果说选择国家,如果类别= 4并且CateCode = 101 我需要结果是美国 由于4存在于Category中,101存在于CateCode中。

谢谢

3 个答案:

答案 0 :(得分:0)

如果您搜索匹配的城市,例如,在表格中输入类别= 3和catcode = 101:

Select city from your_table_name WHERE category=3 AND catcode=101;

答案 1 :(得分:0)

你需要一个不相交的条件。

 select city from your_table_name where  
 catcode=101 OR category=3;

答案 2 :(得分:0)

我猜你想要这样的东西:

SELECT country
FROM theTable
WHERE country IN (SELECT country FROM theTable WHERE category = 4)
   AND country IN (SELECT country FROM theTable WHERE catcode = 101)

代表我认为你正在寻找的更直接/字面意义;但从技术上讲,这应该更快(因为它少了一个子查询):

SELECT country
FROM theTable
WHERE category = 4
   AND country IN (SELECT country FROM theTable WHERE catcode = 101)