试图在MySQL子查询中选择唯一结果的问题

时间:2017-10-13 22:01:05

标签: mysql sql subquery unique

必须编写一个select语句,该语句返回位于唯一城市和州的每个供应商的名称,城市和州。我已经看过几个相似的主题,但由于某种原因,没有给出任何答案。我的代码仍然无法返回正确数量的结果:

  

编写一个select语句,该语句返回位于唯一城市和州的每个供应商的名称,城市和州。它需要返回38行。

/*problem 6*/

SELECT 
vendor_name, vendor_city, vendor_state
FROM
vendors
WHERE
vendor_city || vendor_state NOT IN (SELECT distinct
        vendor_city || vendor_state
    FROM
        vendors
    GROUP BY vendor_city, vendor_state having count(*)>1)
ORDER BY vendor_state , vendor_city;

2 个答案:

答案 0 :(得分:1)

此查询应满足规范的一种可能解释:

  SELECT MIN(v.vendor_name) AS vendor_name
       , v.vendor_city
       , v.vendor_state
    FROM vendors v
   GROUP
      BY v.vendor_city
       , v.vendor_state
  HAVING COUNT(*) = 1

这是获取(city,state)元组,并计算具有相同值的行数,并排除多个行具有相同值的任何行。在剩余的行中,只返回一个vendor_name。

(规范也可能有不同的解释,例如多个vendor_name中的(city,state),我们希望返回仅在vendor_name (city,state)中出现的ONLY_FULL_GROUP_BY那需要不同的查询。)

MySQL中的扩展允许我们在sql_mode中未包含MIN(v.vendor_name)的情况下取消聚合函数,即我们可以仅使用v.vendor_name替换 SELECT t.vendor_name , t.vendor_city , t.vendor_state FROM ( SELECT v.vendor_city , v.vendor_state FROM vendors v GROUP BY v.vendor_city , v.vendor_state HAVING COUNT(*) = 1 ) u JOIN vendors t ON t.vendor_city <=> u.vendor_city AND t.vendor_state <=> u.vendor_state ORDER BY t.vendor_name

如果有一些要求我们必须使用嵌套查询,我更倾向于使用内联视图来返回&#34;唯一城市和州&#34;,然后加入操作

import json, requests 
url=('http://www.theimdbapi.org/api/find/movie?title=transformers&year=2007')
response = requests.get(url) 
dict_values = json.loads(response.text)

答案 1 :(得分:0)

寻求积极的回应通常更有效。因此,不要找到那些拥有超过城市/州的供应商(然后使用NOT来反转它),而是预先做反向,只寻找那些拥有单一城市/州的人。然后使用SELECT v.vendor_name , v.vendor_city , v.vendor_state FROM vendors AS v INNER JOIN ( SELECT vendor_city , vendor_state FROM vendors GROUP BY vendor_city , vendor_state HAVING COUNT(*) = 1 ) AS d ON v.vendor_city = d.vendor_city AND v.vendor_state = d.vendor_state ORDER BY v.vendor_state, v.vendor_city;

对最终输出使用更直接的方法
def query(query):
    client = bigquery.Client()
    query_job = client.run_async_query(str(uuid.uuid4()), query)

    query_job.begin()
    query_job.result()  # Wait for job to complete