Below is my table,
SELECT DISTINCT(availability_location) as location FROM table_name WHERE user_id = '8' ORDER BY availability_date DESC LIMIT 2
I'm getting following result
I want following result :
2016-05-27 pune
2016-05-20 Burbank
i.e. Unique availability_location as well as latest two entries.
You have to use GROUP BY for this:
SELECT availability_location as location,
MAX(availability_date) AS max_date
FROM table_name
WHERE user_id = '8'
GROUP BY location
ORDER BY max_date DESC LIMIT 2
You can use GROUP BY and order by the max date :
SELECT t.availability_location
FROM table_name t
WHERE user_id = '8'
GROUP BY t.availability_location
ORDER BY max(s.availability_date) DESC LIMIT 2
Output :
availability_location
---------------------
pune
Burbank
EDIT: next time, you should mention that you want it to be case sensitive. You can try doing it like this:
SELECT t.availability_location
FROM table_name t
INNER JOIN(SELECT s.availability_location , max(s.availability_date) as max_d
FROM table_name s
WHERE s.user_id = '8'
GROUP BY s.availability_location) t2
ON(t2.availability_location = t.availability_location AND
t2.max_d = t.availability_date)
ORDER BY t.availability_date DESC LIMIT 2
Related
SELECT to_user_id AS sender_id
FROM $chat_table_name
WHERE((to_user_id = $current_user_id OR from_user_id = $current_user_id) and post_id=$post_id)
UNION
SELECT from_user_id AS receiver_id
FROM $chat_table_name
WHERE( (from_user_id = $current_user_id OR to_user_id = $current_user_id) and post_id=$post_id )
order by chat_message_id DESC
How to achieve order by with this query
If I get your question correct, you want to order the indivual SELECTs seperately and you cannot use two ORDER BY clause per each statement instead, add an extra integer column to each SELECT as shown below:
SELECT *, 1 as orderBy from t1
UNION ALL
SELECT *, 2 as orderBy from t2
ORDER BY orderBy desc.
I am making a query in which i want the job ids to be grouped but i want the latest timestamp row in the result which is not happening
Here is the SQL fiddle
http://sqlfiddle.com/#!9/de8769
The normal view for table is
The output after using this query i made
SELECT
DISTINCT(user_id),
job_id,
message,
receiver_id,
parent,
type,
id as id FROM ai_ms_messages
WHERE (receiver_id = '7' OR user_id = '7') AND type<>0 AND type<>2 group by job_id
ORDER BY max(timestamp) DESC
But as you can see its taking the value of id as 3 for job_id 11 but it should have taken the value 5 (as that is latest for job_id 11) and also the order is wrong. Since job_id 11 is latest not job_id 12. Is there any way to achieve this ?
The query would be:
select
distinct(m1.user_id),
m1.job_id,
m1.message,
m1.receiver_id,
m1.parent,
m1.type,
m1.id as id from ai_ms_messages as m1
where m1.type<>0 and m1.type<>2
and m1.timestampt = (select max(m2.timestamp) from ai_ms_messages as m2 where m2.job_id = m1.job_id)
As per your query you are looking for data for receiver_id = '7' and for id =5 , receiver_id = '6' , so this is not in your query output.
Just remove where condition, or check data as per condition only.
GROUP BY groups on the first matching result it hits.
So, its preferable this method as the subquery.
SELECT *
FROM (
SELECT DISTINCT (
user_id
), job_id, message, receiver_id, parent,
TYPE , id AS id
FROM ai_ms_messages
WHERE (
receiver_id = '7'
OR user_id = '7'
)
AND TYPE <>0
AND TYPE <>2
ORDER BY TIMESTAMP DESC
) AS sub
GROUP BY job_id
select name from persons where gender = 'male' order by name limit 10
select name from persons where gender = 'female' order by name limit 10
How can I get the first 10 males and the first 10 females in a single result set (20 entries) with a single SQL query call? Is this possible?
The correct syntax uses parentheses and union all:
(select name
from persons
where gender = 'male'
order by name
limit 10
) union all
(select name
from persons
where gender = 'female'
order by name
limit 10
)
It is possible and it's called UNION
You can use UNION Operator.
UNION is used to combine the result from multiple SELECT statements
into a single result set.
select name from persons where gender = 'male' order by name limit 10
UNION
select name from persons where gender = 'female' order by name limit 10
SELECT name FROM persons WHERE gender = 'male' ORDER BY name LIMIT 0,10 ;
UNION
SELECT name FROM persons WHERE gender = 'female' ORDER BY name LIMIT 0,10 ;
with ROW_NUMBER()
SELECT * FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY gender ORDER BY gender) row_no,* FROM Person
) A WHERE A.row_no <= 10
Here's a query:
SELECT *
FROM table
WHERE id = 1
OR id = 100
OR id = 50
Note that I provided the ids in this order: 1,100,50.
I want the rows to come back in that order: 1,100,50.
Currently, i comes back 1,50,100 - basically in ascending order. Assume the rows in the table were inserted in ascending order also.
Use the MySQL specific FIND_IN_SET function:
SELECT t.*
FROM table t
WHERE t.id IN (1, 100, 50)
ORDER BY FIND_IN_SET(CAST(t.id AS VARCHAR(8)), '1,100,50')
Another way to approach this would put the list in a subquery:
select table.*
from table join
(select 1 as id, 1 as ordering union all
select 100 as id, 2 as ordering union all
select 50 as id, 3 as ordering
) list
on table.id = list.id
order by list.ordering
You can just do this with ORDER BY:
ORDER BY
id = 1 DESC, id = 100 DESC, id = 50 DESC
0 is before 1 in ORDER BY.
Try this
SELECT *
FROM new
WHERE ID =1
OR ID =100
OR ID =50
ORDER BY ID=1 DESC,ID=100 DESC,ID=50 DESC ;
http://www.sqlfiddle.com/#!2/796e2/5
... WHERE id IN (x,y,x) ORDER BY FIELD (id,x,y,z)
When I'm trying to run this query:
select * FROM `activity`
WHERE user_id = 1
AND activity_id NOT LIKE (select activity_id from activity where user_id = 1 ORDER BY activity_id DESC LIMIT 8)
I get the follow error:
Subquery returns more than 1 row
How can I solve this problem? I want to select the activity_id from the table excluding the latest 8 activity_id's for a certain user.
NOT LIKE is expecting an expression or a value to compare against and not a resultset.
Change NOT LIKE for NOT IN
Try this one:
SELECT * FROM `activity`
WHERE user_id = 1 AND activity_id NOT IN (
SELECT activity_id FROM activity WHERE user_id = 1
ORDER BY activity_id DESC LIMIT 8)
Solved it by doing this:
$sql2 = "DELETE t1.*
FROM activity t1
left join (select activity_id from activity where user_id = '".$row['user_id']."' ORDER BY activity_id DESC LIMIT 8) t2
on (t1.activity_id = t2.activity_id)
where t2.activity_id is null
and t1.user_id = '".$row['user_id']."'";