I want to select the last inserted date and at the same time I want to select the user-name and count how many times the user-profile is visited.
So I am using this query
SELECT v.visitor_date, i.info_name, count(DISTINCT v.visitor_date) AS counted
FROM profile_visitors v
INNER JOIN profile_info i ON i.info_userId = v.visitor_accountId
ORDER BY v.visitor_date DESC
LIMIT 1
The result of the fiddle is wrong and SHOULD be
2015-07-28 11:05:16 - Testname - 5
Anyone knows what is wrong with the query?
http://sqlfiddle.com/#!9/2814c/1
DISTINCT does NOT give you the first or last record of any group, in fact you cannot guarantee which record DISTINCT will display within a group (nor does this matter by the way). So select MAX visitor date.
Try below query
SELECT MAX( v.visitor_date ) , i.info_name, COUNT( DISTINCT v.visitor_date ) AS counted FROM profile_visitors v INNER JOIN profile_info i ON i.info_userId = v.visitor_accountId ORDER BY v.visitor_date DESC LIMIT 1
You can try it:
SELECT v.visitor_date,
i.info_name,
COUNT(*) AS counted
FROM profile_visitors v
INNER JOIN profile_info i ON i.info_userId = v.visitor_accountId
GROUP BY v.visitor_accountId
ORDER BY v.visitor_date DESC
LIMIT 1
Related
I have 2 tables
chat_room have two columns : id, slogan
chat have 3 columns : id, chat_room_id,updated_at
this is my code
SELECT chat_room.*
FROM chat_room
ORDER BY (SELECT updated_at FROM chat WHERE chat.chat_room_id = chat_room.id ) DESC;
this is my bug
SELECT chat_room.* FROM chat_room ORDER BY (SELECT updated_at FROM chat WHERE chat.chat_room_id = chat_room.id) DESC LIMIT 0, 1000 Error Code: 1242. Subquery returns more than 1 row 0.00058 sec
The error is telling you that the subquery in the ORDER BY clause sometimes is returning more than one record. This raises the question of which updated_at value you want to use in the case that a given chat room might have more than one value. Assuming you want to sort by the latest updated_at value, you could use:
SELECT cr.*
FROM chat_room cr
INNER JOIN
(
SELECT chat_room_id, MAX(updated_at) AS max_updated_at
FROM chat
GROUP BY chat_room_id
) c
ON c.chat_room_id = cr.id
ORDER BY
c.max_updated_at DESC;
Isn't it better to do a join instead of using a subquery and achieve what you want like this:
SELECT
cr.*
FROM chat_room cr
JOIN chat c on cr.id=c.chat_room_id
order by updated_at desc
I've tried to get last entry with this query but I just can't get last row (only the first):
select users.*,messages.*
from messages
LEFT JOIN users
ON messages.messageBy=users.email
where messageToUser='{loginUser}'
GROUP BY messageBy
If you want to get last record with group by then try the below query
select users.*,message.*
from
message
LEFT JOIN
users
ON
message.messageBy = users.email
where
messageToUser=1
AND
messageid IN (SELECT max(messageid) FROM message GROUP BY messageBy)
GROUP BY
messageBy
ORDER BY
messageid DESC
rough demo at http://sqlfiddle.com/#!2/cda6f/1
So, if you have any kind of id field or date stored in your message table, you could do:
SELECT
...
ORDER BY your_date_field DESC
LIMIT 1
OR
SELECT
...
ORDER BY id DESC
LIMIT 1
Otherwise you are left with using code to count the number of records returned and just get the last one.
Oh, you want the last message from each user.
A stock solution looks like this...
SELECT x.*
FROM my_table x
JOIN (SELECT my_grouping_id,MAX(my_ordering_id) max_my_ordering_id FROM my_table GROUP BY my_grouping_id) y
ON y.my_grouping_id = x.my_grouping_id
AND y.max_my_ordering_id = x.my_ordering_id;
Take the below for example:
SELECT
*
FROM
auctions AS a
LEFT JOIN winners AS w ON a.auction_id=w.auction_id
WHERE
a.active=1
AND
a.approved=1
GROUP BY
a.auction_id
ORDER BY
a.start_time DESC
LIMIT
0, 10;
Sometimes this may match multiple results in the winners table; I don't need both of them, however I want to have control over which row I get if there are multiple matches. How can I do an ORDER BY on the winners table so that I can make sure the row I want is the first one?
It is difficult to accurately answer without seeing your table structure but if your winners table has a winner date column or something similar, then you can use an aggregate function to get the first record.
Then you can return the record with that earliest date similar to this:
SELECT *
FROM auctions AS a
LEFT JOIN winners w1
ON a.auction_id=w1.auction_id
LEFT JOIN
(
select auction_id, min(winner_date) MinDate -- replace this with the date column in winners
from winners
group by auction_id
) AS w2
ON a.auction_id=w2.auction_id
and w1.winner_date = w2.MinDate
WHERE a.active=1
AND a.approved=1
ORDER BY a.start_time DESC
SELECT *
FROM auctions AS a
LEFT JOIN (select auction_id from winners order BY auction_id limit 1) AS w ON a.auction_id = w.auction_id
WHERE a.active = 1
AND a.approved = 1
GROUP BY a.auction_id
ORDER BY a.start_time DESC
Change the reference to the winners table in the join clause to a sub-query. This then gives you control over the number of records returned, and in what order.
I've got query like:
SELECT
b.title,
b.url,
b.`date`,
b.gallery,
count(c.id) as comments_count,
a.name,
b.content,
b.comments,
LEFT(b.content, LOCATE('<page>', b.content)-1) as content_short
FROM blog b
LEFT JOIN blog_comments c ON
(b.id = c.note AND c.approved = 1)
LEFT JOIN administrators a ON
(b.aid = a.id)
WHERE
b.`date` < now() AND
b.active = 1
ORDER BY b.`date` DESC;
Now, when I remove count(c.id) as comments_count,, I've got 2 rows returned. When it's present, there's only 1 row returned.
Is there some way to fix ot or I simply have to change
count(c.id) as comments_count, to (select count(id) ascomments_countfrom blog_comments where note = b.id) as comments_count,?
Count(*) is an aggregated function, so it will apply in a group.
That means that when you count on groups, it will apply the function on every group.
The groups are formed when you use Group By, in this case, you're not using, so MySQL consider that ALL select (your joins) is ONLY 1 GROUP.
So, applies the count on the unique group and returning the count of rows.
you should add a Group by by the field you want
An example is here
I have this query:
SELECT id_user, COUNT(*) as count
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
which gives me the id_user ordered by occurrences, and the number of each occurrence.
Can I get, in the same request, the LAST post from each 'id_user'? i.e. I want to select the last 'post' too, but when I do
SELECT id_user, post, COUNT(*) as count
Tthe value in 'post' isn't the last one (nor the first one; actually I don't know how groups are ordered). Should I run another query?
I believe u can accomplish this by adding max(post_id) last_post to your select.
This ought to do it in one query:
SELECT
p.id_user,
ap.post AS last_post,
COUNT(*) as count
FROM
posts p
JOIN posts ap on (
p.id_user = ap.id_user
AND ap.post_id = (
SELECT MAX(post_id) FROM posts ip WHERE p.id_user = ip.id_user
)
GROUP BY
p.id_user,
ap.post
ORDER BY
COUNT(*) DESC