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
Related
Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();
I don't know why it's not working... I'm trying to "order by" a created variable like this:
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time FROM ab_list WHERE time = '24/08/2013' ORDER BY banfrom DESC LIMIT 0,50");
Or,
$hr = date('d/m/Y');
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time FROM ab_list WHERE time = '$hr' ORDER BY banfrom DESC LIMIT 0,50");
Assuming the variable $hr is in the format %d/%m/%Y, you can use having instead of where to filter the records.
mysql_query("SELECT *,DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') as time
FROM ab_list
HAVING time = '$hr'
ORDER BY banfrom DESC
LIMIT 0,50");
The reason to use HAVING and not WHERE in this case is because time is computed column.
It's because you are using an ALIAS in the WHERE clause that was created on the same level of the SELECT clause. There are two ways to make it working,
One, using the calculated column directly in the WHERE clause
WHERE DATE_FORMAT(FROM_UNIXTIME(banfrom), '%d/%m/%Y') = '24/08/2013'
Second, using a subquery
SELECT a.*
FROM (put your whole query here) a
WHERE a.time = '24/08/2013'
WHERE cannot use ALIAS because in the SQL Order of Operation the WHERE clause is executed before the SELECT clause. Here's the list of order of operations:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause (Alias is created here)
ORDER BY clause
This query giving strange result:
SELECT `user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND (`rankType` = "top5"
OR `rankType` = "top20")
ORDER BY rankType
LIMIT 0 , 30
here the SQLfiddle.
Want I am trying to achieve is:
1)To get only 5 records of top5 rank type, 20 records of rank type top20
2)I want to show the result in ascending order of rank type.(but if you see in the demo fiddle it's showing apposite, may be it is only considering 2 from 20 & 5)
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5"
ORDER BY rankType
LIMIT 0, 5)
union
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20"
ORDER BY rankType
LIMIT 0, 20)
If later on you want to add another set of sorting/filtering columns, wrap it all into something like
select * from ( /* previous query goes here */ ) tt
where id > 100
order by id
Note that ranktype is varchar, so it's sorted lexicographically, so top20 < top5. You'll have to employ natural sorting or some other means to get it right.
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5" limit 5
union
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20" limit 20
Your result is actually in ascending order, since column rank_type is of varchar type top20 comes first than top5 as in string comparison.
If you only want to deal between top5 and top20, a dirty solution could be:
ORDER BY rankType desc
One possibility without doing two queries and UNIONing them:
ORDER BY FIND_IN_SET(rankType,'top5,top20')
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
Simply I have the following
table with records , i want to return randomly 5 records from the last 20 record (order by id desc)
so how we can do it fast
thanks for help.
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. This may be slow though.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow,
There are multiple alternative that you can read about here
Alternative to order by rand
You need something like
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this.
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5;