Using common order by for both query with union function - mysql

Friends i'm using two queries with same concept in the below query i've used two order by function instead i would like to write only one order by function for the whole query using same attribute date_time DESC is it possible in mysql
(SELECT * FROM `chats` WHERE `chat_from` = '18' AND `chat_to` = '13' ORDER BY `date_time` DESC )
UNION
(SELECT * FROM `chats` WHERE `chat_from` = '13' AND `chat_to` = '18' ORDER BY `date_time` DESC )

Try This
SELECT * FROM `chats`
WHERE `chat_from` IN(18,13)
AND `chat_to`IN(18,13)
ORDER BY `date_time` DESC

select * from
(
SELECT * FROM `chats` WHERE `chat_from`in ('18','13') and `chat_to` in ('18','13')
)
t1 ORDER BY `date_time` DESC

You could do something like this:
SELECT * FROM `chats`
WHERE
(
`chat_from` = '18'
AND `chat_to` = '13'
)
OR
(
`chat_from` = '13'
AND `chat_to` = '18'
)
ORDER BY `date_time` DESC
If you need them to be distinct then you can use:
SELECT DISTINCT * FROM `chats`...

Related

How get the least ID from table after GROUP BY

I want to get recent messages of user. And I need get LAST ID of message to each a fetch. How can I do it? Thank you!
Structure:
Query:
SELECT *
FROM `messages`
WHERE (
`from` = 1 OR
`to` = 1
)
GROUP BY (`from` + `to`)
ORDER BY `id` DESC
LIMIT 10
Result:
What I need?
If you have same a problem? I find solution!
SELECT `id`, `from`, `to`, `text`, `created`
FROM (
SELECT *, (`from` + `to`) AS `anchor`
FROM `messages`
WHERE (
`from` = 1 OR
`to` = 1
)
ORDER BY `id` DESC
LIMIT 1
) AS `*`
GROUP BY `anchor`;
If I understand correctly, you can use a correlated subquery:
select m.*
from messages m
where 1 in (m.from, m.to) and
m.id = (select max(m2.id)
from messages m2
where (m2.from = m.from and m2.to = m.to) or
(m2.from = m.to and m2.to = m.from)
);
Note: from and to are really bad names for columns, because they are SQL keywords.

Execute query that normally runs on a single ID for every ID

So I have this query:
SELECT ABS(
TIMESTAMPDIFF(MINUTE, NOW(),
(SELECT MAX(`Time`) FROM ddHistorical WHERE ID = '5')
)
)
I want to run that on every ID in my database, how would I do that?
This works (http://www.sqlfiddle.com/#!2/e8ac0/3/0)
SELECT Abs(Timestampdiff(minute, Now(), (SELECT Max( ` time ` )
FROM ddhistorical
WHERE id = a.id
GROUP BY id)))
FROM ddhistorical AS A
GROUP BY id
Try this:-
SELECT ABS(TIMESTAMPDIFF(MINUTE, NOW(), (SELECT MAX(`Time`) FROM ddHistorical

How do I combine these two Select queries with an OR case

I want to select all rows where WHERE (uid = {$uid} OR uid = **HERE** ) where **HERE** is the cids retreived from query 2 below.
Query 1:
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` = **HERE** )
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
And Query 2:
SELECT `cid` FROM `t_con` WHERE `uid` = {$uid} AND `flag` = 1
SELECT * FROM `t_activities`
WHERE (`uid` = {$uid} OR `uid` in (SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1))
AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
You can do this as a join as well:
SELECT *
FROM `t_activities` ta left outer join
(SELECT `cid`
FROM `t_con`
WHERE `uid` = {$uid} AND `flag` = 1)
) tc
on ta = tc.cid
WHERE (`uid` = {$uid} OR tc.`uid` is not null) AND `del` = 0
GROUP BY `fid`
ORDER BY `time` DESC
LIMIT 10
By the way, as a SQL statement the "GROUP BY fid" looks very strange. This is allowed in mysql, but I think it is a bad practice. It is much better to be explicit about what you are doing:
SELECT fid, min(<field1>) as Field1, . . .
This helps prevent mistakes when you go back to the query or try to modify it.

mysql date-format returns null

I have the following SQL query. It returns NULL for the 'invoice_date'.
SELECT * , fk_uid AS userId, fk_ivid AS invoice_uid, (
SELECT company
FROM tbl_users
WHERE pk_uid = userId
) AS invoice_customer, (
SELECT DATE_FORMAT( '%d/%m/%y', purchased ) AS invoice_date
FROM _invoices
WHERE invoice_uid = invoice_uid
LIMIT 1
) AS invoice_date
FROM tbl_statement_items
WHERE statement_generated = '1'
AND fk_statementId = '1'
LIMIT 0 , 30
Any help would be appriciated.
Thanks
You have inverted date_format parameters order.

Why can i sort an inline SELECT value but not use it in a WHERE clause?

I have this small SQL query.
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
AND `grade` >= 5
ORDER BY `grade` DESC
This gives me the error
1054 - Unknown column 'grade' in 'where clause'
But if i remove the 2nd last line, it works fine. I have tried to do AND a.grade and even give the tests table a name and append that name to grade but still no luck.
How can I use this inline query in a WHERE clause?
I have found that this works, but is it the only way?
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
AND (
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) >= 5
ORDER BY `grade` DESC
Sql statements are somewhat evaluated in the following order:
FROM
WHERE
SELECT
GROUP
HAVING
ORDER
So things you define in the SELECT-clause are not available in the WHERE-clause. You would need to put that constraint into a HAVING-clause:
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
HAVING `grade` >= 5
ORDER BY `grade` DESC
SELECT a.`id` , a.`title` , a.`date` ,
(
SELECT MAX( grade )
FROM tests
WHERE userid = 41
AND presid = a.`id`
) AS grade
FROM `presentations` a
WHERE a.`visible` = 1
HAVING `grade` >= 5
ORDER BY
`grade` DESC