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
Related
I have a question with SQL.
I have 3 tables, messages, payments and reports.
I want to get all events from a user from every table in one query, so this is wat I like to get:
SELECT * FROM messages WHERE receiver_id = 10
SELECT * FROM payments WHERE client_id = 10
SELECT * FROM reports WHERE receiver_id = 10
But in 1 query.
Is this possible?
Table structures:
- messages
id
sender_id
receiver_id
type
text
created_at
updates_at
- payments
id
user_id
client_id
method
cost
status
text
created_at
updated_at
- reports
id
sender_id
receiver_id
text
created_at
updates_at
If there is no relationships between the 3 tables then you can just use UNION. It can be as simple as this:
SELECT receiver_id as event_id, event_name, event_description, event_timestamp FROM messages WHERE receiver_id = 10
UNION
SELECT client_id as event_id, event_name, event_description, event_timestamp FROM payments WHERE client_id = 10
UNION
SELECT receiver_id as event_id, event_name, event_description, event_timestamp FROM reports WHERE receiver_id = 10
This assumes there is a common set of columns across all tables that you are interested in. If however the columns have differing names but contain the same data (and datatypes), then you can use AS to rename the columns in the query to achieve the UNION
SELECT receiver_id as event_id, col1 as event_name, col2 as event_description, col3 event_timestamp FROM messages WHERE receiver_id = 10
UNION
SELECT client_id as event_id, col_a as event_name, col_b as event_description, col_c event_timestamp FROM payments WHERE client_id = 10
UNION
SELECT receiver_id as event_id, col_x as event_name, col_y as event_description, col_z event_timestamp FROM reports WHERE receiver_id = 10
Keep in mind that UNION will eliminate duplicate rows in your dataset. If this is not the behavior that you want then use UNION ALL instead (it will not eliminate duplicates).
Also keep in mind that if you column datatypes are not the same across the table then you will have to cast them to a common type. Casting is highly dependent of your DBMS so make sure you use the correct methods to do so.
Its fairly straight forward providing receiver_id and client_id hold the same values:
SELECT * FROM `messages` LEFT JOIN `payments` ON
`messages`.`receiver_id` = `payments`.`client_id` LEFT JOIN `reports`
ON `messages`.`receiver_id` = `reports`.`receiver_id`
I dont really understand what you want, do you want to get all the data in 1 query, or do you need joins with them?
If you want all the data in one table you can just do this i think:
SELECT * FROM messages as M, payments as P, reports as R
WHERE M.receiver_id, P.client_id, R.receiver_id = 10
Else you need to show us some more details about the relationship
You can use Union or Union All to concatenate the results sets of the 3 queries. However all 3 tables must have the same amount of columns for it to work. Post your tables structure for a more exact solution.
SELECT * FROM messages WHERE receiver_id = 10
Union All
SELECT * FROM payments WHERE client_id = 10
Union All
SELECT * FROM reports WHERE receiver_id = 10
I want to use 1 table to create a new table using 2 sets of queries.
To test out the code: http://sqlfiddle.com/#!9/02e3ff/5
Reference table:
Desired table:
They share the same order_id.
type = A, updated_at = pDate
type = B, updated_at = dDate
Query 1:
select t.order_id, t.updated_at as pDate, weekday(t.updated_at) from transactions t
where t.type = 'A' group by t.order_id
Query 2:
select t.order_id, max(t.updated_at) as dDate, weekday(max(t.updated_at)) from transactions t
where t.type= 'B'
group by t.order_id;
For type = A, I want to get the earliest updated_at date, while for type = B, I want to get the latest updated_at date.
Currently, I tried union but they give me 2 rows instead of the desired table.
How do I join or union these 2 queries to get the desired table?
Alternatively, is there a better method to do this? Thanks!
You can try something like this:
SELECT order_id, min(pDate) pDate, max(dDate) dDate FROM(
SELECT
order_id,
if(type='A',updated_at,null) pDate,
if(type='B',updated_at,null) dDate
FROM transactions
) as d
GROUP BY order_id
SQLFiddle
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
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']."'";
I have a table called items_status which has 3 fields, item_id, user_id, and status, which can be either 'have' or 'want'.
Field Type Null Key
user_id varchar(10) NO PRI
item_id varchar(10) NO PRI
status set('have','want') YES NULL
I have a page where I want to get a list of all the user ids in the table ordered by the number of records their user id is associated with in the table where status is 'have'. So far, this is the best I can come up with:
SELECT user_id
FROM items_status AS is
ORDER BY
//Subquery to get number of items had by user
(SELECT COUNT(i.item_id)
FROM items_status AS i
WHERE i.user_id = is.user_id AND i.status = 'have') DESC
GROUP BY user_id
However, this pulls up an error on the subquery. How can I get all of the user ids in the table ordered by the number of items they have?
you can do it like this:
SELECT user_id
FROM items_status
WHERE `status` = 'have'
GROUP BY userID
ORDER BY COUNT(user_id) DESC
SQLFiddle Demo
with slight difference of column name but the query is the same
SELECT user_id, SUM(CASE WHEN i.status = 'have' THEN 1 ELSE 0) AS s
FROM items_status AS is
GROUP BY user_id
ORDER BY SUM(CASE WHEN i.status = 'have' THEN 1 ELSE 0) DESC