hi i have one database and i use this query for get latest records using group by and and order by
i have use this code for get all record
SELECT id,time FROM eventlog WHERE event = '2' ORDER BY `eventlog`.`id` DESC
and i get this output:
i want to get latest time of every group
i have use this query for group by with order by query
SELECT id,time FROM eventlog WHERE event = '2' GROUP BY id ORDER BY time DESC
Try this:
SELECT id,MAX(`time`) as `time` FROM eventlog WHERE event = '2' GROUP BY id DESC
Related
I have records like this
here is my query
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10)
OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC
But here order now is not working and this is the result I am getting after running this above query
You have not used any aggregate function , so your group by is just returning 1st data set. There are several ways to fix it
Remove group by and just use order by if we you want to sort by conversation_id
Use aggregate function
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10) OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC LIMIT 0,1
I am trying to get data from my table using group by . using group by works correctly but i need to take only last inserted item of every group but its not work. my query always return first item of each group.
my query
SELECT id,type,userId,performDate,eventId FROM
`user_marker` where `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d')
and `visibility`='1'GROUP BY type ORDER BY id DESC
Please try
SELECT a.* FROM ( SELECT id,type,userId,performDate,eventId FROM
`user_marker` where `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d') and `visibility`='1' ORDER BY id DESC ) as a GROUP BY a.type
You can try as per below-
SELECT b.id,b.type,b.userId,b.performDate,b.eventId
FROM user_marker b
JOIN (SELECT `type`,MAX(performDate)
FROM user_marker
WHERE `eventId`='842' AND DATE(FROM_UNIXTIME(performDate))=CURDATE() AND `visibility`='1'
GROUP BY `type`) a ON a.type=b.type AND a.performDate=b.performDate
ORDER BY b.`type`;
I have an table like this
I have been try this sql code
SELECT id,lat,lng,name,MIN(hitung) AS Smallest FROM open_list;
but the result give me wrong query
what i wanna do is being like this :
You could do this:
SELECT id,lat,lng,name,hitung
FROM open_list
ORDER BY hitung ASC
LIMIT 1
Or, if you want to do more complex stuff, start with this:
SELECT id,lat,lng,name,hitung
FROM open_list
JOIN (
SELECT MIN(hitung) as hitung
FROM open_list
) tmp USING (hitung)
When you do not GROUP BY non-aggregate values from your SELECT list, the returned values are arbitrary. You can add a GROUP BY but that will return multiple records, you can use ORDER BY and LIMIT to get what you're after:
SELECT id,lat,lng,name,hitung
FROM open_list
GROUP BY id,lat,lng,name
ORDER BY hitung ASC
LIMIT 1;
I have a basic write to and retrieve SQL database PHP "thing" and I want to have the output in descending order. How do I do that?
For example, the last entry is shown first, then the entry before that, then the entry before that, etc. The first entry ever is last.
Use:
SELECT field_name
FROM table_name
ORDER BY id DESC
By default, MySQL will show results in ascending order. If you want to show them in reverse order, use ORDER BY field_name DESC.
You can use id or date as the field name.
Change the ORDER BY statement
from ORDER BY col or ORDER BY col ASC or to ORDER BY col DESC
from ORDER BY col DESC to ORDER BY col ASC
Sort using DESC ORDER BY.
SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC
If there's an auto increment field, you order by that field, descending.
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
That's from ORDER BY Clause - Sort Data In SQL.
Write the below query to retrieve data:
SELECT * FROM `table_name` order by id desc
It will retrieve data from the table in descending order.
MySQL general query syntax for SELECT:
SELECT field1, field2,...fieldN FROM table_name
[WHERE Clause][GROUP BY]
[ORDER BY][LIMIT]
Example query 1:
SELECT id,first_name,last_name,email FROM users ORDER BY first_name desc // In this case you can only fetch data by = id,first_name,last_name,email
Example query 2:
SELECT * FROM users ORDER BY first_name desc // In this case all fields will be selected
Note: ORDER BY ASC = Data will sort by Ascending Order & ORDER BY DESC = Data will sort by descending Order
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