简体   繁体   中英

counting number of rows in mysql column

I have a table which looks like,

 | user_id | url | is_bookmark | is_history    <== schema

sample data:

[2, "http://www.stackoverflow.com", 1 ,0]
[3, "http://www.stackoverflow.com", 1 ,0]
[4, "http://www.stackoverflow.com", 1 ,1]
[5, "http://www.php.net", 1 ,0]

and need the following table,

 |  url | total_bookmark | total_history    <== schema

sample data for this table

["http://www.stackoverflow.com",  3 , 1] 
["http://www.php.net",  1 , 0] 

I do this using 2 queries, but can it be done in 1?

try this:

 select url , count(*) total_bookmark, sum(is_history) total_history
 from yourtable
 group by url

DEMO

Use aggregate function sum on both the columns bookmark and history.

If any of them are unset from book marked or history status, count will not work but sum works.

Try this:

select 
    url,
    sum( is_bookmark ) total_bookmark,
    sum( is_history ) total_history
from table
group by url

Did u mean this ?

select count(*) as total,is_bookmark,url,sum(is_history) total_history
from table
group by `url`

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