我如何计算这个mysql UNION查询

时间:2017-11-10 21:21:01

标签: mysql union-all

这是我的查询,我仍然有点挂起mysql连接。我想得到这个查询的计数....

SELECT *
FROM `users`
WHERE id = '4'
UNION
SELECT *
FROM `users`
WHERE id IN 
(SELECT group_id 
FROM `users` 
WHERE id = '4')
AND status = '1'

2 个答案:

答案 0 :(得分:0)

您可以计算子查询

  select count(*) from (

  SELECT *
  FROM `users`
  WHERE id = '4'
  UNION
  SELECT *
  FROM `users`
  WHERE id IN 
  (SELECT level 
  FROM `users` 
  WHERE id = '4')
  AND status = '1') t

答案 1 :(得分:0)

我同意scaisEdge要使用的代码。但是,您在子查询中尝试做什么?这似乎是寻找ID,但您选择了' Level'对于IN声明?除非LEVEL与ID相同,否则它不会做任何事情。

SELECT *
FROM `users`
WHERE id IN 
(SELECT level 
FROM `users` 
WHERE id = '4')
AND status = '1'