I want to get the most recent row from each inner joined tables. Both tables have a timestamp field. Below is what I have so far. But it only targets for table1 how about for table2?
SELECT
`table1`.`fieldX`,
`table2`.`fieldY`
FROM `db`.`table1`
INNER JOIN `db`.`table2`
ON `table1`.`id` = `table2`.`id`
WHERE `table1`.`id` = ?
ORDER BY `table1`.`timestamp`
DESC LIMIT 1
table1
row_id
id
fieldX
timestamp
table2
row_id
id
fieldY
timestamp
Both tables can have repeating ids. It was designed this way to store older versions of the data entries.
For example: table1 can have 3 rows with the same id while table2 can have 2 rows of the same id. I want to get the latest row from both tables.
Use can use it like this if you want recent record from table2
SELECT SUBSTRING_INDEX(GROUP_CONCAT(t1.fieldX ORDER BY t1.timestamp DESC),',',1) as field_x, SUBSTRING_INDEX(GROUP_CONCAT(t2.fieldY ORDER BY t2.timestamp DESC),',',1) as field_y
FROM table1 t1
JOIN table2 t2 ON(t2.id = t1.id)
GROUP BY t1.id
ORDER BY t1.timestamp
Related
Table 1:
user score
------------
A 1
B 2
Table 2:
user comment time
----------------------------
A good <timestamp 1>
A bad <timestamp 2>
B average <timestamp 3>
I want to join these two tables such that I get the below:
user score comment
-------------------------
A 1 good
B 2 average
As you can see I'll need to join the second table's comment based on the timestamp (the most recent timestamp). I tried
SELECT st.user as user,st.score,
case when v.comment is null then 'NA' else v.comment end as comment
FROM tale1
left JOIN (select distinct user,comment,max(time) from table2) v ON st.user=v.user
but this doesnt work.
You can join with a correlated subquery that filters on the latest timestamp:
select
t1.*,
t2.comment
from table1 t1
left join table2 t2
on t2.user = t1.user
and t2.time = (
select max(t22.time)
from table2 t22
where t21.user = t1.user
)
Side note: I am unsure that you do need a left join here (your sample data does not demonstrate that).
You only want one column from table2 so I recommend a correlated subquery:
select t1.*,
(select t2.comment
from table2 t2
where t2.user = t1.user
order by t2.time desc
limit 1
) as comment
from table1 t1;
This query will make optimal use of an index on table2(user, time desc, comment) -- alas, though, I think the desc is ignored in MySQL.
The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
I have two separate queries which pretty much return the same thing:
select id
from t
where id<>''
GROUP BY id
having count(*) >= 2;
select id
from t2
where id is not null
GROUP BY id
having count(*) >= 2
ORDER BY id ASC;
a list of ids whose values appear more than once. The first query returns more than the second, So I need to left join them somehow to get the results that are in the first query but not in the second. I tried to do a left join but it is not working properly.
I also tried the following to no avail:
select id
from t
where id<>''
GROUP BY id
having count(*) >= 2
not in (select id from t2 where id is not null GROUP BY id having count(*) >= 2
ORDER BY id ASC)
Additional Info
Query one is giving me all ids that have the same value for table 1 and query 2 is giving me all the same value ones for table 2. There are additional gotchas like there are some blank ids in table one while there are some nulls in table 2, hence the conditions excluding blanks for the former and nulls for the latter. So I get back these two separate results, which are almost the same, except in results 1 there are claims that arent in results 2 but only when these queries are run because they are duplicated in table 1 but not in table 2. although they do exist in table 2. So a simple left join where t1.id <> t2.id will not work because they do exist in t2.
You want to select ids in t1 that are not in t2. JOIN on t2 and ensure that the result is NULL.
SELECT t.id
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id IS NULL
GROUP BY t.id
HAVING COUNT(t.id) > 1
SELECT t.id, t2.id FROM t
LEFT OUTER JOIN t2
ON t.id <> t2.id
WHERE t.id<>''
GROUP BY t.id having count(t.id) >= 2
ORDER BY t.id ASC
I assume your not in part before the subquery is actually mean that the id from the table t and t2 are not same and so added the condition t.id <> t2.id
EDIT
SELECT t.id FROM t
WHERE t.id<>''
AND t.id NOT IN (SELECT id FROM t2 where id is not null )
GROUP BY t.id having count(t.id) >= 2
ORDER BY t.id ASC
SQLFIDDLE
I have a database with many tables.. and each table has stored only IDs. Now what I want to do is:
SELECT id FROM table1, table2, table3
GROUP BY id;
but I also want to sort them by decreasing order of occurrence.
For example the IDs that are in all 3 tables should appear on top and the IDs appearing in only one table should be at the bottom. Any clue on how to do this?
Try this too
select id from
(
SELECT id FROM table1
union all
select id from table2
union all
select id from table3
) as t
GROUP BY id
order by count(id) desc
select sum(t1.id IS NOT NULL,t2.id IS NOT NULL, t3.id IS NOT NULL) as total,t1.id from table1 as t1 join table2 as t2 on t1.id=t2.id join table3 as t3 on t3.id = t1.id order by total desc;
I am not sure about your problem but this can help
select id from
(
SELECT id FROM table1
union all
select id from table2
union all
select id from table3
) as t
GROUP BY id
order by id desc
I have two different tables
in first one (t1) I have
Table [t1]
id Product_URL
.
Table [t2]
id Product_id Product_URL
I would like to UPDATE ALL product_id field (from t2) to the id of the first
WHERE t1.product_url = t2.product_url
can I do that In one query?
UPDATE t2
JOIN (
SELECT t2_2.id, t1.id as new_id
FROM
t2 t2_2 JOIN
t1 ON t2_2.product_url = t1.product_url AND t2_2.product_id <> t1.id
ORDER BY t2_2.id
LIMIT 5000
) sub ON t2.id = sub.id
SET id = sub.new_id;
EDIT: It looks like mutli-table updates do not play well with LIMIT and ORDER BY, but here's another query that accomplishes the same thing...