Im wondering if someone could help out.
I need to write a query that gets the last 3 'created' records, but their UID has to be unique so for example, my mysql fields look like so
uid created
19 2012-02-01 01:08:43
18 2012-02-31 17:07:21
19 2012-02-31 16:07:20
20 2012-02-31 13:03:00
Ok, so i want to get the last 3 uid's created ... but they have to be unique uid's so the same uid cant appear twice.
Cheers
this should do this
SELECT * FROM ( SELECT * FROM tablename ORDER BY uid, created DESC ) ordered GROUP BY uid
You can select last 3 IDs with this query:
SELECT * FROM ( SELECT * FROM table ORDER BY uid, created DESC ) AS selected GROUP BY uid LIMIT 3
Logic is: order table by uid field in descending and limit to 3 fields.
Use a Distinct and Group by to get what you're after:
SELECT DISTINCT * FROM table GROUP BY uid;
That will give you all unique UID's.
Then add your ORDER BY in there to make sure you grab the last records.
SELECT *
FROM table
ORDER BY created DESC
GROUP BY uid LIMIT 0,3
SELECT uid
, MAX(created) AS max_created
FROM tableX
GROUP BY uid
ORDER BY max_created DESC
LIMIT 3
Related
I want to select last 5 records from first 50 records in the table, currently i have following query, somebody tell me best way to select these records without calculating the limit and offset?
SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45
Try this
SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC
this works:
SELECT id FROM (SELECT id,date FROM table ORDER BY date LIMIT 50) AS
temptable ORDER BY date DESC LIMIT 5
i have one table named users. In this table i have one column named credits(not unique).
Now i want the second highest user accroding to the users credits. If the credits field is unique thenmy below query is working fine
SELECT * FROM users ORDER BY credits DESC LIMIT 1 , 1
But , if the users credit is not unique then its create problem for retrive me data
suppose,
mack has 200 credits
jack has 200 credits
rock has 150 credits
when i has this types of record then,in output of this query i want the rock record not jack
can anyone help me to find out the correct value ?
thanks in advance
SELECT a.*
FROM users a
INNER JOIN
(
SELECT DISTINCT credits
FROM users
ORDER BY credits desc
LIMIT 1,1
) b ON a.credits = b.credits
SQLFiddle Demo
Hope this helps (first get second highest credits then find the users having those that credit andselect one from the top`. This will retrieve one user having second highest credit):
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1,1)
LIMIT 1;
EDIT: If you also want to select within users having same score then use the appropriate filter/sorting condition e.g. to select rock between rock and jenni, you could have another ordering base on name(assuming name is the column having names)
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1,1)
ORDER name desc
LIMIT 1;
To get both rock and jenni, just remove the limit from the end and update the inner limit e.g:
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1, 1);
try this
Select * FROM users
Where Credits < (Select Max(Credits) From Users)
ORDER BY credits DESC LIMIT 1;
I have a sql query for getting first 40 list of users.I want to retrieve one user always in that list.Is their any method in query specifying the user id with the limit
The best way I can think of is:
SELECT * FROM tbl WHERE userid='your-user-id' UNION SELECT * FROM tbl WHERE userid!='your-user-id' LIMIT 39
Basically, you select your user, and then you select 39 others. You use UNION to conjoin the two SELECT results.
SELECT
*
FROM
`users`
WHERE
`user_id` != 12345
LIMIT 39
UNION SELECT
*
FROM
`users`
WHERE
`user_id` = 12345
ORDER BY `user_id`
;
This will give you first 39 users + user with user_id=12345.
Select * from table order by userd_id limit 40
I guess that would be
(SELECT * from users limit 39)
UNION ALL
(SELECT * from users where userid='your-user-id')
As most of the options are already provided, check if this can help you
select name from user_details where id = user_id || id != user_id order by field (id,user_id) desc limit 40;
This will give you combine results & if you want your specified user id will always come on top.
i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order
I want to select the newest posts from users, I am looking for the most efficient way to do this.
Currently this selects the first post, not the last:
$query = mysql_query("
SELECT *
FROM posts
WHERE toID=fromID
GROUP BY fromID
ORDER BY date DESC LIMIT 3");
Table Structure:
Table: posts
id ToID FromID Post State Date
1 1 1 Hey 0 1325993600
2 1 6 okay yeah 0 1325993615
3 1 2 again 0 1325994600
4 6 6 yeah2 0 1325995615
so from this above example it would return id: 1 and 4.
toID=fromID is just to get the post that is a status message, meaning the user posted something on their own page, not someone elses.
I want to get the most recent status from the last 3 users that have updated their status.
The ID thing would still work theoretically, provided that the ID's never change...
I would recommend using a timestamp field in the table structure called "date" and use the "CURRENT_TIMESTAMP" as default value, this will auto-populate the date/time on the record upon insert...
Order by this field DESC, limit x
Also, I have experienced many cases of the wrong data appearing thanks to grouping... Make sure your data is correct before ORDER BY and LIMIT is applied
For getting posts from user1 to user1 there's no need to group by:
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC LIMIT 3
For getting posts from * to user1:
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC LIMIT 3
For getting posts from * to user1, only unique users:
SELECT * FROM posts
WHERE toID="USER1_ID"
GROUP BY FromID
ORDER BY date DESC LIMIT 3
Somtimes you will run into the problem where GROUPED records are not ordered by ORDER BY, because the ORDER BY is applied to the result AFTER the grouping is applied... To achieve a workaround:
SELECT * FROM (
SELECT * FROM posts
WHERE toID="USER1_ID"
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
To Get the last 3 users who have most recently sent themselves a post:
SELECT * FROM (
SELECT * FROM posts
WHERE toID=fromID
ORDER BY date DESC
) as `derived` GROUP BY FromID LIMIT 3
try this query.
$query = mysql_query("SELECT * FROM posts WHERE toID=fromID GROUP BY id ORDER BY date DESC LIMIT 3");