MySQL查询是否可能返回true/false而不是值?

17
我有一个表格:
custID    orderID    orderComponent
=====================================
1          123        pizza
1          123        wings
1          234        breadsticks
1          239        salad
2          456        pizza
2          890        salad

我有一个值列表 - 披萨、翅膀、面包棒和沙拉。我需要一种方法,只要客户至少有一个包含这些元素的记录,就可以获得真/假值。在mysql查询中是否可能实现这一点,或者我只能针对每个用户执行select distinct(orderComponent)并使用php来检查结果?


根据你所拥有的数据,你希望从查询中得到什么结果? - Eric Petroelje
我认为 custID 1 将返回 true,而 custID 2 将返回 false。 - George
4个回答

14

如果您只是想查看客户是否已经订购了所有商品,那么您可以使用以下方法:

select t1.custid,
  case when t2.total is not null 
    then 'true'
    else 'false'
  end OrderedAll
from yourtable t1
left join
(
  select custid, count(distinct orderComponent) Total
  from yourtable
  where orderComponent in ('pizza', 'wings', 'breadsticks', 'salad')
  group by custid
  having count(distinct orderComponent) = 4
) t2
  on t1.custid = t2.custid

请参见 带演示的 SQL Fiddle

如果您想扩展这个查询,以查看 custid 是否已在一次订单中订购了所有商品,则可以使用以下语句:

select t1.custid,
  t1.orderid,
  case when t2.total is not null 
    then 'true'
    else 'false'
  end OrderedAll
from yourtable t1
left join
(
  select custid, orderid, count(distinct orderComponent) Total
  from yourtable
  where orderComponent in ('pizza', 'wings', 'breadsticks', 'salad')
  group by custid, orderID
  having count(distinct orderComponent) = 4
) t2
  on t1.custid = t2.custid
  and t1.orderId = t2.orderid

查看具有演示的SQL Fiddle

如果您只需要custid和true/false值,则可以将distinct添加到查询中。

select distinct t1.custid,
  case when t2.total is not null 
    then 'true'
    else 'false'
  end OrderedAll
from yourtable t1
left join
(
  select custid, count(distinct orderComponent) Total
  from yourtable
  where orderComponent in ('pizza', 'wings', 'breadsticks', 'salad')
  group by custid
  having count(distinct orderComponent) = 4
) t2
  on t1.custid = t2.custid

请参见带演示的SQL Fiddle

或按照custid和orderid:

select distinct 
  t1.custid,
  t1.orderid,
  case when t2.total is not null 
    then 'true'
    else 'false'
  end OrderedAll
from yourtable t1
left join
(
  select custid, orderid, count(distinct orderComponent) Total
  from yourtable
  where orderComponent in ('pizza', 'wings', 'breadsticks', 'salad')
  group by custid, orderID
  having count(distinct orderComponent) = 4
) t2
  on t1.custid = t2.custid
  and t1.orderId = t2.orderid

请查看具有演示的 SQL Fiddle


所以,此查询并不为custID返回单个true/false值,而是为清单中具有订单组件的每一行返回true/false值。正确吗? - EmmyS
是的,它会为每行添加一个true/false。你只想要custid和true/false吗? - Taryn
顺便说一句,感谢提供 SQLFiddle 的链接。我经常使用 jsfiddle,但不知道还有一个用于 SQL 的工具。非常有用。 - EmmyS
"true" 是字符串类型。是否可能返回布尔值 true? - Muaaz Khalid
@muaaz 可能你需要尝试一下。 - Taryn
显示剩余4条评论

8
select case when 
count(distinct orderComponent) = 4 
then 'true' 
else 'false' 
end as bool
from tbl
where custID=1

那行不通,因为列表不总是有4个项目。 - EmmyS

2
这里有一种方法。这种方法不需要内联视图(派生表),如果您想要为多个条件包括标志,则可以有效地使用它:

编辑:

这将返回具有四个项目的 custID 的行:

SELECT t.custID
     , MAX(IF(t.orderComponent='breadsticks',1,0))
       + MAX(IF(t.orderComponent='pizza',1,0))
       + MAX(IF(t.orderComponent='salad',1,0))
       + MAX(IF(t.orderComponent='wings',1,0)) AS has_all_four
  FROM mytable t
 GROUP BY t.custID
HAVING has_all_four = 4

翻译后的文本:

(这个检查了一个拥有所有四个项目的客户“订单”,而不仅仅是“custID”。)

SELECT t.custID
     , t.orderID
     , MAX(IF(t.orderComponent='breadsticks',1,0))
       + MAX(IF(t.orderComponent='pizza',1,0))
       + MAX(IF(t.orderComponent='salad',1,0))
       + MAX(IF(t.orderComponent='wings',1,0)) AS has_all_four
  -- , MAX(IF(t.orderComponent='breadsticks',1,0)) AS has_breadsticks
  -- , MAX(IF(t.orderComponent='pizza',1,0)) AS has_pizza
  -- , MAX(IF(t.orderComponent='salad',1,0)) AS has_salad
  -- , MAX(IF(t.orderComponent='wings',1,0)) AS has_wings
  FROM mytable t
 GROUP BY t.custID, t.orderID
HAVING has_all_four = 4

这将获取具有所有四个项目的“订单”。如果您只想返回custID的值,则可以将上面的查询用作内联视图(将其包装在另一个查询中)

SELECT s.custID
  FROM (
         SELECT t.custID
              , t.orderID
              , MAX(IF(t.orderComponent='breadsticks',1,0))
                + MAX(IF(t.orderComponent='pizza',1,0))
                + MAX(IF(t.orderComponent='salad',1,0))
                + MAX(IF(t.orderComponent='wings',1,0)) AS has_all_four
             FROM mytable t
            GROUP BY t.custID, t.orderID
           HAVING has_all_four = 4
       ) s
 GROUP BY s.custID

@EmmyS。这是我的错。该查询检查包含所有四个项目的“订单”。要检查顾客,只需从GROUP BY和SELECT列表中省略orderID即可。我会更新我的答案。 - spencer7593

0

@EmmyS:你可以两种方式都做。如果你想使用MySql进行检查,请使用:

SELECT @rowcount:=COUNT(*) FROM orderComponent Where (Your Conditions);
IF (@rowcount > 0) THEN
    'True'
ELSE
    'False'
END IF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接