I'm attempting to get a list of values that don't exist in another table for a specific date.
Table Layouts:
table1 (TABLE)
id INT(10)
table2 (TABLE)
id INT(10)
table1id INT(10)
dateupdated DATETIME(19)
SQLFiddle
Query that I thought would work that isn't.
SELECT t1.id FROM Table1 as t1
JOIN Table2 as t2
ON t1.id=t2.table1id
WHERE date(t2.dateupdated) != DATE(NOW())
I'm expecting id 7 and 8 to come back from the above query, since they don't exist in Table2. But I'm getting no rows back.
Reversing the Query to match = DATE(NOW()) gives me the lines that do have matches
SELECT t1.id FROM Table1 as t1
JOIN Table2 as t2
ON t1.id=t2.table1id
WHERE date(t2.dateupdated) = DATE(NOW())
Response:
id
1
2
3
4
5
6
9
10
Any guidance would be appreciated, I'm still learning MySQL. But I'm missing something very simple here.
http://sqlfiddle.com/#!9/7a9fc/11
SELECT t1.id FROM Table1 as t1
LEFT JOIN Table2 as t2
ON t1.id=t2.table1id
AND date(t2.dateupdated) = DATE(NOW())
WHERE t2.id IS NULL;
Since there is always more than one way, another way is using the NOT IN operator with a subquery (since MySQL doesn't support MINUS).
SELECT t1.id FROM Table1 as t1
WHERE t1.id NOT IN
(SELECT t2.table1id FROM Table2 as t2
WHERE date(t2.dateupdated) = DATE(NOW()))
For DBMS that do support MINUS, you could do:
SELECT t1.id FROM Table1 as t1
MINUS
SELECT t2.table1id FROM Table2 as t2
WHERE date(t2.dateupdated) = DATE(NOW())
This is the way that I prefer to do it:
SELECT id FROM Table1
WHERE id not in (select table1id from Table2 where date(dateupdated) = date(NOW()))
Learning mysql I found this invaluable reference: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
Hope you find it useful!
Since the JOIN doesn't retrieve NULL rows, you have to use a LEFT or RIGHT JOIN to deal with empty ROWS.
Related
I want to get the id from first table, and the information in the second table but only if there is any related row in second table, AND where the greatest date of the related rows is smaller(earlier) than time right now...
This is what I have right now:
"SELECT t1.id as actID, t2.id AS eventsid, t2.controlled FROM `activites` t1
LEFT JOIN `events` t2 ON t2.activitesid = t1.id
WHERE t2.datum < UNIX_TIMESTAMP() GROUP BY t2.controlled"
And what I have been trying is something like
"SELECT t1.id as actID, t2.id AS eventsid, t2.controlled FROM `activites` t1
LEFT JOIN `events` t2 ON t2.activitesid = t1.id
WHERE (SELECT `datum` FROM `events` t3 WHERE t3.controlled = t2.controlled ORDER BY `datum` DESC LIMIT 1) < UNIX_TIMESTAMP() GROUP BY t2.controlled"
Is there anybody who can point me in wich direction to go or how to type this kind of query?
There is no need pointing out that I should use PDO, mysqli etc...
I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4
I have two database tables - table1 and table2. For some records in table1 i have several rows connected in table2. For most of them i have 3 rows connected, but for some of them i have an extra row with a column value like table2.field='correct'. How can i join table1 and table2 if i want the result to return only the rows from table1 where there is no row in table2 with column value like table2.field='correct' connected to them ? Counting the number of rows from the second table( if num of connected rows < 4 or something like that is not an option).
I tried something like :
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL
but it did not work ofc. because i always have rows with value in field column. For each row in t1 that is connected to t2 i have record rows in t2 where t2.filed='name' and t2.field='type'. I need the rows from t1 that do not have a row connected to them in t2 where t2.field='correct'.
Use NOT IN
SELECT * from t1 WHERE t1.id NOT IN(SELECT id FROM t2 WHERE t2.field = 'correct')
$sql = "SELECT t1.* FROM table1 t1 CROSS JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL ";
SELECT t1.* FROM table1 t1 WHERE NOT EXISTS (SELECT t2.* FROM table2 t2 WHERE t1.id=t2.id_t1 AND t2.field = 'correct')
I have a problem...I have 2 tables, Table1 and Table2.
Table1:
id,int(11)
text,varchar(11)
Table2:
id,int(11)
agentid,int(11)
unique(id,agentid)
Table2 has many id and agent ids in it. The data for the id in table 2 came from table 1.
I have an agentid say $aid. This has many id's associated with it in table 2.
I am trying to get the set of text values out from table 1 associated with all ids which are related to agentid $aid from table 2.
Does that make sense?!
Any ideas?
select text from table1 where id in
(
select id from table2 where agentid = <your aid>
)
This will give you all text rows for given agentid.
The same can be done using join too -
select t1.text from table1 t1
inner join table2 t2
on t1.id = t2.id
where t2.agentid = <your aid>
select * from table1 as t1 inner join table2 as t2
on t1.id = t2.id
where agentid = $aid
The query:
$sql = "select t1.text from table1 t1, table2 t2 where t2.id = t1.id and t2.agentid = {$aid}";
Try not to include the $aid directly in the query, but escape it and/or use prepared statements with query parameters.
Hi I do have an old table and new table with same index/data. TABLE1 and TABLE2
but TABLE1 has got more data than TABLE2. this was maintained by some one and I dont know how this happened. so my question is how do I compare these two table and find which data is TABLE2 missing?? there is almost 200000 datas there so manually doing is not possible...
in PHP:
http://us.php.net/manual/en/function.array-diff.php
in SQL:
SELECT * FROM TABLE1 WHERE id {NOT} IN ( SELECT id FROM TABLE2 )
depending on criteria of comparison
Solution without nested query:
select TABLE1.id from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id where TABLE2.id is null
Did you mean something like this:
SELECT * FROM TABLE1 t1 WHERE NOT EXISTS(SELECT * FROM TABLE2 WHERE t1.id == t2.id)
By same index I am hoping you mean they share a primary key?
SELECT * FROM TABLE1 WHERE username NOT IN (SELECT username FROM TABLE2)