Is it possible for a mysql query to return true/false instead of values? Is it possible for a mysql query to return true/false instead of values? mysql mysql

Is it possible for a mysql query to return true/false instead of values?


If you are just looking to see if the customer has ordered all items, then you can use:

select t1.custid,  case when t2.total is not null     then 'true'    else 'false'  end OrderedAllfrom yourtable t1left 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

See SQL Fiddle with Demo

If you want to expand this out, to see if the custid has ordered all items in a single order, then you can use:

select t1.custid,  t1.orderid,  case when t2.total is not null     then 'true'    else 'false'  end OrderedAllfrom yourtable t1left 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

See SQL Fiddle with Demo

If you only want the custid and the true/false value, then you can add distinct to the query.

select distinct t1.custid,  case when t2.total is not null     then 'true'    else 'false'  end OrderedAllfrom yourtable t1left 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

See SQL Fiddle with Demo

Or by custid and orderid:

select distinct   t1.custid,  t1.orderid,  case when t2.total is not null     then 'true'    else 'false'  end OrderedAllfrom yourtable t1left 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

See SQL Fiddle with Demo


select case when count(distinct orderComponent) = 4 then 'true' else 'false' end as boolfrom tblwhere custID=1


Here's one approach. This approach does not require an inline view (derived table), and can be effective if you want to include flags for multiple conditions:

EDIT:

This returns custID that has a row for all four items:

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.custIDHAVING has_all_four = 4

ORIGINAL ANSWER:

(This checked for a customer "order" that had all four items, rather than just a "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.orderIDHAVING has_all_four = 4

That will get the "orders" that have all four items. If you want to return just values for custID, then use the query above as an inline view (wrap it in another query)

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