this is my query
SELECT a.id,
a.venue_id,
a.user_id,
m1.profilenam AS user_profilename,
m1.photo_thumb AS user_photo_thumb,
m2.profilenam AS venue_profilename,
m2.photo_thumb AS venue_photo_thumb
FROM announce_arrival AS a
INNER JOIN members AS m1
ON a.user_id = m1.mem_id
INNER JOIN members AS m2
ON a.venue_id = m2.mem_id
GROUP BY a.venue_id, a.user_id
LIMIT 0,10
ORDER BY date DESC,
time DESC
How can i use count(*) on this query,i use like this
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC;
but its showing
COUNT(*)
7
3
1
i want total count .
You can wrap your query into select count(*), like this:
SELECT COUNT(*) FROM
(SELECT a.id,a.venue_id, a.user_id, m1.profilenam as
user_profilename,m1.photo_thumb AS user_photo_thumb,m2.profilenam AS
venue_profilename, m2.photo_thumb AS venue_photo_thumb FROM announce_arrival
AS a INNER JOIN members as m1 ON (a.user_id = m1.mem_id) INNER JOIN members
as m2 ON (a.venue_id= m2.mem_id) GROUP BY a.venue_id, a.user_id)
You can wrap your query in another SELECT:
SELECT COUNT(*) AS total FROM (
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
or if you want sum of all DISTINCT COUNT(*) try:
SELECT SUM(cnt) AS total FROM (
SELECT DISTINCT COUNT(*) AS cnt
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
From docs about found_rows():
To obtain this row count, include a
SQL_CALC_FOUND_ROWS option in the
SELECT statement, and then invoke
FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The second SELECT returns a number
indicating how many rows the first
SELECT would have returned had it been
written without the LIMIT clause.
I.e., add SQL_CALC_FOUND_ROWS after SELECT in your first query and replace second query with SELECT FOUND_ROWS().
Get rid of GROUP BY, LIMIT and ORDER. They are useless and don't make sense (especially LIMIT) if you need a total count. DISTINCT doesn't make sense either.
SELECT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
Try this
SELECT COUNT(a.user_id)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.user_id LIMIT 0,10;
If you are using count no need to give order by
Related
This is my code i am trying to left join the latest team data, not every piece of data. i have tried just using limit 1 but doesnt return anything
ORDER BY updated DESC LIMIT 1
this doesnt work
Any ideas?
$sql = "SELECT
events.id, events.time,events.status, events.home_team,events.away_team,events.league,
ht.id as home_id,ht.name as home_name,at.name as away_name,
statistics.home_goals,statistics.away_goals,statistics.time as game_time,
leagues.id as league_id,leagues.name as league_name,leagues.type as league_type,
country.name as country_name,country.logo,
hts.home_scored, ats.away_scored,
hts.home_conceeded,ats.away_conceeded,
hts.home_win,ats.away_win,
hts.home_15,ats.away_15,
hts.home_25,ats.away_25,
hts.home_btts, ats.away_btts,
hts.home_fts, ats.away_fts,
hts.home_cs, ats.away_cs,
hts.home_corners_for, ats.away_corners_for,
hts.home_corners_against, ats.away_corners_against,
hts.home_cards, ats.away_cards
FROM events
LEFT JOIN teams ht
ON ht.id = events.home_team
LEFT JOIN teams at
ON at.id = events.away_team
LEFT JOIN leagues
ON leagues.id = events.league
LEFT JOIN country
ON country.id=leagues.country
LEFT JOIN ( SELECT team,home_scored,home_conceeded,home_win,home_15,home_25,home_btts,home_fts,home_cs,home_corners_for,home_corners_against,home_cards FROM team_quick_stats ORDER BY updated DESC) hts
ON ht.id=hts.team
LEFT JOIN ( SELECT team,away_scored,away_conceeded,away_win,away_15,away_25,away_btts,away_fts,away_cs,away_corners_for,away_corners_against,away_cards FROM team_quick_stats ORDER BY updated DESC) ats
ON at.id=ats.team
LEFT JOIN statistics
ON statistics.event_id=events.id
WHERE (events.time BETWEEN $start AND $end) ORDER BY country.list_order, leagues.country ASC , leagues.id ASC, events.time ASC, home_name ASC";
Here's one way. Replace LEFT JOIN (SELECT team... etc....) ats with...
LEFT
JOIN
( SELECT x.team
, x.etc...
FROM team_quick_stats x
JOIN
( SELECT team
, MAX(updated) updated
FROM team_quick_stats
GROUP
BY team
) y
ON y.team = x.team
AND y.updated = x.updated
) ats...
i create a web app like facebook by php and mysqli
in my app i have a table for posts , one table for likes , and one table for comments
i want to get the number of comments and likes of each post in one row with his post_id!!!
i try some querys likes this :
select `tblpost`.`post_id`, COALESCE(TCOMM.`comment_num`,0) as `c_num`, COALESCE(TLIKE.`like_num`,0) as `l_num`
from
(select `tblpost`.`post_id`, count(*) as `like_num` from `tblpost` join `tbllikes` on `tbllikes`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`
) TLIKE
inner join
(select `tblpost`.`post_id`, count(*) as `comment_num` from `tblpost` join `tblcomments` on `tblcomments`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`) TCOMM
on
TCOMM.`post_id` = TLIKE.`post_id`
but i don't know what's my problem
You can do count distincts with two left joins.
Something like this would work if there are fields like_id and comment_id in the tables tbllikes and tblcomments
SELECT
tblpost.post_id AS post_id,
COUNT(DISTINCT tbllikes.like_id) AS likes,
COUNT(DiSTINCT tblcomments.comment_id) AS comments
FROM tblpost
LEFT JOIN tbllikes ON tbllikes.post_id = tblpost.post_id
LEFT JOIN tblcomments on tblcomments.post_id = tblpost.post_id
GROUP BY tblpost.post_id
First, I think you can greatly simplify your query:
select l.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from (select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l inner join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on l.post_id = c.post_id;
This will only get you posts that have both likes and comments. To get what you want, use a left join:
select p.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from tblpost p left join
(select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l
on l.post_id = p.post_id left join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on c.post_id = p.post_id;
Okay, so I have a query below I am trying to get to work.
Basically everything works up until the inner join of the 'votes' table. What I am trying to do is order the results of this query in accordance with the number of votes each content row has in another table called votes. I know I'm not too far off from what I need to do!
Thanks in advance!!
mysql_query("
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN votes ON votes.id = content.id
WHERE (content.type = 'pic')
ORDER BY COUNT(votes.id) DESC");
Try doing:
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN (
SELECT id,COUNT(*) as voteCount
FROM votes
GROUP BY id
) v ON v.id = content.id
WHERE (content.type = 'pic')
ORDER BY v.voteCount DESC
When you do an INNER JOIN with votes table directly, if you have multiple occurrences of the same id, you will get a lot more rows than before you did the JOIN.
If you are only interested in the number of votes for each id, by doing a JOIN with a subquery that calculates the count of votes for each id, will leave your previous query results as they were, and lets you use the voteCount to order by it.
You could try this:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id) votes
FROM content c
INNER JOIN users u ON c.uploaderuid = u.id
INNER JOIN votes v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY votes DESC
Here is the SQL Fiddle that demonstrates the below query:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id)
FROM content AS c
INNER JOIN users AS u ON c.uploaderuid = u.id
INNER JOIN votes AS v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY COUNT(v.id) DESC
I am trying to get the required result from the following query but it doesnt seem to work...
SELECT DISTINCT
u.user_name as user_name,
u.total_points as total_points,
u.user_id as user_id,
COUNT(a.id) as user_total_articles_published,
COUNT(r.id) as user_total_replies_published,
COUNT(v.id) as user_total_votes_done
FROM users as u
LEFT JOIN articles as a ON u.user_id=a.user_id
LEFT JOIN replies as r ON u.user_id=r.user_id
LEFT JOIN votes as v ON u.user_id=v.user_id
GROUP BY u.user_id
ORDER BY u.total_points DESC
LIMIT 10
If i remove the last 2 LEFT jOINS the query will work... whats wrong with the other 2? Do i have to use another method for this to work?
thanks
I think by 'not working' you mean that the query returns too many records? That is because of the combination of joins. You return each reply for each article record, so the numbers are multiplied. You can solve this by using DISTINCT in the COUNT. That way, you count the unique id's, so you count each article only once:
COUNT(distinct a.id) as user_total_articles_published,
COUNT(distinct r.id) as user_total_replies_published,
COUNT(distinct v.id) as user_total_votes_done
[edit]
A possibly faster solution, eliminating the need for DISTINCT and GROUP BY:
SELECT
u.user_name as user_name,
u.total_points as total_points,
u.user_id as user_id,
(SELECT COUNT(a.id) FROM articles a
WHERE a.user_id = u.user_id) as user_total_articles_published,
(SELECT COUNT(r.id) FROM replies r
WHERE r.user_id = u.user_id) as user_total_replies_published,
(SELECT COUNT(v.id) FROM votes v
WHERE v.user_id = u.user_id) as user_total_votes_done
FROM users as u
ORDER BY u.total_points DESC
LIMIT 10
Hello can anybody see why this query fails?
SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset
But this one works:
SELECT *FROM ads
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset
In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.
If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.
SELECT
A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
F.*,
D.*,
S.*,
C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset