简体   繁体   English

SQL条件查询表

[英]Sql conditional query on table

I have 2 tables. 我有2张桌子。

I am trying to get all columns from articles - and the number of status where status = 0 as status0 - and the number of status where status = 1 as status1 我正在尝试从articles获取所有列-并将status = 0的状态数作为status0并将status = 1的状态数作为status1

"Get all from articles and for each article row get the number of comments where status = 0 as status0 and get the number of comments where status = 1 as status1". “从文章中获取所有内容,并为每个文章行获取status = 0作为status0的注释数,并获取status = 1作为status1的注释数”。 Possible? 可能?

Tables: 表:

articles
========
id   name
---------
1    abc
2    def
3    ghi


comments
========
id   article_id    status
-------------------------
1    2             1
2    2             0
3    1             0
4    3             1

Expected result of articles combined with status numbers: 带有状态编号的文章预期结果:

id   name    status0   status1
------------------------------
1    abc     1         0
2    def     1         1
3    ghi     0         1

I am using Eloquent of Laravel, but would be sufficient to see the raw sql statement. 我正在使用Laravel的Eloquent,但足以看到原始sql语句。 I have no clue how to query and count these status. 我不知道如何查询和计算这些状态。


Thanks to fiddle I have managed to create this query, but I get an error: Note that ( articles = db_surveys and comments = db_answers ) 多亏了小提琴,我设法创建了这个查询,但是出现一个错误:请注意,( articles = db_surveyscomments = db_answers

"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`status=1) status1` from `db_surveys` left join `db_answers` on `db_surveys`.`i' at line 1 (SQL: select `db_surveys`.`*, SUM(db_answers`.`status=0) status0, SUM(db_answers`.`status=1) status1` from `db_surveys` left join `db_answers` on `db_surveys`.`id` = `db_answers`.`surveyid` where `db_surveys`.`userid` = 6oGxr)"

Full query: 完整查询:

"select `db_surveys`.`*, SUM(db_answers`.`status=0) status0, SUM(db_answers`.`status=1) status1` from `db_surveys` left join `db_answers` on `db_surveys`.`id` = `db_answers`.`surveyid` where `db_surveys`.`userid` = 123 group by `db_surveys`.`id`"

** **

Final query: 最终查询:

** **

SELECT 
  `s`.*,
   SUM(`a`.`status`='pending') `status0`, 
   SUM(`a`.`status`='confirmed') `status1` 
FROM
  `db_surveys` s
  LEFT JOIN `db_answers` a
    ON `s`.`id` = `a`.`surveyid` 
WHERE `s`.`userid` = '6oGxr' 
GROUP BY `s`.`id` 

You can use sum() with expression to get the count on basis your conditions,using expression in sum will result as boolean o or 1 您可以将sum()与expression一起使用以根据您的条件获取计数,使用sum中的expression将得到布尔o或1

SELECT a.*
,SUM(`status` =0) status0   
,SUM(`status` =1) status1   
FROM articles a
LEFT JOIN comments c ON(a.id = c.article_id)
GROUP BY a.id

Fiddle Demo 小提琴演示

Edit In your original query you are not using back-ticks properly 编辑在原始查询您不使用回蜱正确

SELECT 
  `s`.*,
   SUM(`a`.`status`=0) `status0`, 
   SUM(`a`.`status`=1) `status1` 
FROM
  `db_surveys` s
  LEFT JOIN `db_answers` a
    ON `s`.`id` = `a`.`surveyid` 
WHERE `s`.`userid` = 123 
GROUP BY `s`.`id` 

Possibly something like this: 可能是这样的:

SELECT a.*, COUNT(c.*) AS status0, COUNT(c2.*) AS status1
FROM articles AS a
LEFT JOIN comments AS c ON c.article_id = a.id AND c.status = 0
LEFT JOIN comments AS c2 ON c.article_id = a.id AND c.status = 1

Not the pretiest one, but it work : 不是最漂亮的,但它可以工作:

SELECT articles.id, articles.name, 
     (SELECT COUNT(*) FROM comments WHERE article_id = articles.id AND status = 0), 
     (SELECT COUNT(*) FROM comments WHERE article_id = articles.id AND status = 1) 
FROM articles;

http://sqlfiddle.com/#!2/123b68/4 http://sqlfiddle.com/#!2/123b68/4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM