2

我有以下 SQL:

SELECT `table1`.`value`, `table2`.* 
FROM `table2` 
INNER JOIN `table1` ON `table2`.`product_id` = `table1`.`entity_id`
WHERE `table2`.`created_at` > '2012-04-23' and 
(`table1`.`value` = 264 OR `table1`.`value` = 260)
order by order_id

它返回一个这样的结果集(这只是返回结果的一部分):

value  order_id   ...
260    1234
260    1235
260    1236
264    1236
260    1237
260    1238
260    1239
264    1239
264    1240
260    1241

我想要的是一个查询,它将获取这些结果,并且只返回 order_id 包含值 260 和 264 的订单。基于这个例子,我正在寻找的最终结果是

260   1236
264   1236
260   1239
264   1239

我的想法是这可以用一个子集来完成,但我不确定完成它的细节。

4

4 回答 4

5

这可以通过关系划分来完成:

select r.order_id from (
  select 
    dividend.*  
  from your_table_or_query as dividend  -- assumes no duplicates in `dividend`; use `distinct` if there are any
  inner join divisor
  on dividend.value = divisor.value
) as r
group by r.order_id
having count(*) = (select count(*) from divisor);

结果:

+----------+
| order_id |
+----------+
|     1236 |
|     1239 |
+----------+
2 rows in set (0.00 sec)

your_table_or_query您的查询在哪里

select 260 as value from dual union select 264 as value from dual

divisor

这将返回订单 ID 1236 和 1239;然后可以将它们join编入原始查询以获取具有这些订单 ID 的所有行,如果这是您想要的。


完整查询以及插入语句:

create table divisor (value int);
insert into divisor values (260), (264);

create table your_table_or_query (value int, order_id int);
insert into your_table_or_query values (260, 1234), (260, 1235), (260, 1236), (264, 1236), (260, 1237), (260, 1238), (260, 1239), (264, 1239), (264, 1240), (260, 1241);


select y.* from (
  select r.order_id from (
    select
      dividend.*
    from your_table_or_query as dividend
    inner join divisor
      on dividend.value = divisor.value
  ) as r 
  group by r.order_id
  having count(*) = (select count(*) from divisor)
) as quotient
inner join your_table_or_query y
  on quotient.order_id = y.order_id;

结果:

+-------+----------+
| value | order_id |
+-------+----------+
|   260 |     1236 |
|   264 |     1236 |
|   260 |     1239 |
|   264 |     1239 |
+-------+----------+
4 rows in set (0.00 sec)
于 2012-05-07T17:26:44.683 回答
1

这个怎么样?

SELECT table1.value, table2.* 
FROM table2 
INNER JOIN table1 
ON table2.product_id = table1.entity_id
WHERE table2.created_at > '2012-04-23' AND
(table1.value = 264 OR table1.value = 260)
AND table2.order_id IN (
    SELECT table2.order_id  
    FROM table2 
    INNER JOIN table1 
    ON table2.product_id = table1.entity_id
    WHERE table2.created_at > '2012-04-23' AND
    (table1.value = 264 OR table1.value = 260)
    GROUP BY table2.order_id
    HAVING COUNT(*) = 2
)

(很抱歉省略了撇号,我不知道该字段中的 mysql 要求是什么;此语法符合 ms-sql)

于 2012-05-07T17:14:39.303 回答
1

这将返回同时具有值​​ 260 和 264 的订单列表 - 如果有人只需要此结果而不是每个值的重复结果:

SELECT `table2`.* 
FROM `table2` 
INNER JOIN `table1` as `table1_264`
  ON `table2`.`product_id` = `table1_264`.`entity_id`
  AND `table1_264`.`value` = 264
INNER JOIN `table1` as `table1_260`
  ON `table2`.`product_id` = `table1_260`.`entity_id`
  AND `table1_260`.`value` = 260
WHERE `table2`.`created_at` > '2012-04-23'
order by order_id


或者如果有人想要更快的解决方案而不需要昂贵的having条款(+当然你可以存储查询或使用 CTE 而不是重复它)

SELECT 264 as `value`, `table2`.* 
 FROM `table2` 
 INNER JOIN `table1` as `table1_264` ON `table2`.`product_id` = `table1_264`.`entity_id` AND `table1_264`.`value` = 264
 INNER JOIN `table1` as `table1_260` ON `table2`.`product_id` = `table1_260`.`entity_id` AND `table1_260`.`value` = 260
 WHERE `table2`.`created_at` > '2012-04-23'
UNION ALL
SELECT 260, `table2`.* 
 FROM `table2` 
 INNER JOIN `table1` as `table1_264` ON `table2`.`product_id` = `table1_264`.`entity_id` AND `table1_264`.`value` = 264
 INNER JOIN `table1` as `table1_260` ON `table2`.`product_id` = `table1_260`.`entity_id` AND `table1_260`.`value` = 260
 WHERE `table2`.`created_at` > '2012-04-23'
order by order_id
于 2012-05-07T18:02:42.383 回答
-1
SELECT `table1`.`value`, `table2`.* 
FROM `table1` 
LEFT JOIN `table2` ON `table1`.`entity_id` = `table2`.`product_id`
WHERE
(`table1`.`value` = 264 OR `table1`.`value` = 260)
AND `table2`.`created_at` > '2012-04-23' 
ORDER BY `table2`.order_id

左连接,因此它仅将 table1.value 结果与匹配项匹配,并将其他表中的信息留在外面

于 2012-05-07T17:04:23.923 回答