简体   繁体   中英

Need to get one row of result with subselect

have sql query like this:

SELECT 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 1") AS test_1 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 2") AS test_2 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 3") AS test_3 
FROM `table_name` GROUP BY `content`

and getting result like this:

test_1, test_2, test_3
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0
2,      7,      0

Need to get result just one row, like this:

test_1, test_2, test_3
2,      7,      0

How? What's wrong with my query?

Check This.

SELECT  distinct 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 1") AS test_1 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 2") AS test_2 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 3") AS test_3 
FROM `table_name` GROUP BY `content`

Your query should be rewrite like this:

SELECT 
    SUM(CASE WHEN `content` = "test 1" THEN 1 ELSE 0 END) AS test_1,
    SUM(CASE WHEN `content` = "test 2" THEN 1 ELSE 0 END) AS test_2,
    SUM(CASE WHEN `content` = "test 3" THEN 1 ELSE 0 END) AS test_3
FROM 
    `table_name`
WHERE 
    `custom_id` IN (10,9,8,6,5,4,3,2,1);

Just add the TOP clause in your SELECT statement...

SELECT TOP 1
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 1") AS test_1 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 2") AS test_2 , 
(SELECT COUNT(`id`) FROM `table_name` WHERE `custom_id` IN (10,9,8,6,5,4,3,2,1) AND `content` = "test 3") AS test_3 
FROM `table_name` GROUP BY `content`

What you are doing doesn't make much sense. You select from the table and group by content. That means you get one result row per content (one for 'test 1', one for 'test 2' etc.). There seem to be ten different content in your table, as you show that you get ten result rows.

Then for every such row, no matter what it contains, you are executing your three subqueries that always get the same results of course.

So either:

select 
  (select count(*) from table_name 
    where custom_id in (10,9,8,6,5,4,3,2,1) and content = 'test 1') as test_1 , 
  (select count(*) from table_name 
    where custom_id in (10,9,8,6,5,4,3,2,1) and content = 'test 2') as test_2 , 
  (select count(*) from table_name
    where custom_id in (10,9,8,6,5,4,3,2,1) and content = 'test 3') as test_3 ;

or better:

select 
  sum(content = 'test 1') as test_1,
  sum(content = 'test 2') as test_2,
  sum(content = 'test 3') as test_3
from table_name
where custom_id in (10,9,8,6,5,4,3,2,1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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