简体   繁体   中英

Count number of rows for specific ID

In first table album has id and second table album_details has sub_id which relates from album table id

在此处输入图片说明

在此处输入图片说明

I need to display count for separate id value.

SELECT DISTINCT B.SUB_ID, A . * , B.CONTENT_VALUE AS detail, 
(SELECT COUNT( ID ) 
FROM album_details WHERE A.ID = B.SUB_ID ) AS count
FROM album AS A, album_details AS B
WHERE A.WEBSITE_ID =  '571710720'
AND A.ID = B.SUB_ID
GROUP BY B.SUB_ID
LIMIT 0 , 30

Now count column shows 40 for all rows but need to display 'count' 6 for 'id=4', 'count' 3 for 'id=2'

SELECT count( SUB_ID ),SUB_ID从album_details组按SUB_ID

Lets say first table is A and second table is B then query will be like this

select a.ID, count(b.SUB_ID) AS total
FROM A LEFT JOIN B ON A.ID = B.SUB_ID
Group by B.SUB_ID.

It might help you. If not then ask please.

GROUP BY is your weapon of choice.

SELECT 
    a.ID,
    a.CONTENT_VALUE,
    COUNT(ad.ID)
FROM albums AS a
LEFT JOIN album_details AS ad ON a.ID = ad.SUB_ID
GROUP BY a.ID

Feel free to add your WHERE before the GROUP BY .

从album_details中选择count(sub_id)作为count1,其中sub_id in(从专辑中选择id)在哪里WHERE album.WEBSITE_ID ='571710720'AND album.ID = album_details.SUB_ID

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