Multiple data check in one query - mysql

I have the following problem. I have 2 tables in the database - table1 and table2.
Table1
id| val1| val2
--------------
1 | 234 | 342
2 | 325 | 356
...
Table2
id | uid | val
--------------
1 | 5 | 234
2 | 6 | 362
3 | 5 | 123
I would like to check for each record in table2 if val exists in table1 (table2.val=table1.va1 or table2.val > table1.vall).
In table1 is about 2 million records. In table2 several thousand.
If query result true i'd like to remove rows from table2.
Is it possible to do this in one query? mysql or postgresql
Performance is very important.

Assuming (t2.val = t1.val1) or (t1.val2 > t2.val) conditions:
delete table2
from table2 t2
inner join table1 t1 on (t2.val = t1.val1) or (t1.val2 > t2.val)

delete t
from #table2 t
inner join #table1 t2
on t.val = t2.val or t2.val>t.val

Assuming uid in Table2 reference id in Table1 (and that neither table contain nulls), this returns the rows in Table1 that satisfy your existence criteria:
SELECT *
FROM Table1
WHERE EXISTS (
SELECT *
FROM Table2
WHERE Table2.uid = Table1.id
AND (
Table2.val = Table1.val1
OR Table2.val > Table1.val2
)
);

Related

How to get distinct values from a Table not present in multiple table

I am trying to get a unique list of values from a table which is not present in the corresponding columns in other 2 tables( multiple tables)
Here is how my Table looks :
----------- ----------- -----------
Table1 Table2 Table3
---|------- ---|------- ---|-------
id | Value id | Value id | Value
---|------- ---|------- ---|-------
1 | A 1 | A 1 | A
2 | B 2 | C 2 | D
3 | C 3 | D 3 | E
4 | D 4 | G 4 | F
Now, the unique value of Table1 is "B" ( This value is not present in Table2 and Tabl3).
Similarly unique value of Table2 is "G". Similarly unique value of Table3 is "E, F".
I am using the following query :
select Value from Table1 where Table1.Value NOT IN (select Value from Table2);
Any idea how to extend into 2 tables ( or more)?
Thanks
Use and:
select Value
from Table1
where Table1.Value not in (select Value from Table2) and
Table1.Value not in (select Value from Table3) ;
I discourage the using NOT IN with subqueries. It doesn't behave as you would expect with NULL values. If any value in the subquery is NULL, then all rows are filtered out.
Instead, use NOT EXISTS:
select t1.Value
from Table1 t1
where not exists (select 1 from table2 t2 where t2.value = t1.value) and
not exists (select 1 from table3 t3 where t3.value = t1.value);
You may also use left joins here:
SELECT t1.Value
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.Value = t2.Value
LEFT JOIN Table3 t3
ON t1.Value = t3.Value
WHERE
t2.Value IS NULL AND t2.Value IS NULL;

SQL select MAX from other table

I have two tables.
table1:
| ID | NAME |
|----|------|
| 1 | aaa |
| 2 | aaa |
| 3 | aaa |
| 4 | bbb |
| 5 | bbb |
table2:
| ID | DATE |
|----|----------|
| 1 | 12/07/10 |
| 2 | 12/07/13 |
| 3 | 12/07/16 |
| 4 | 12/07/08 |
| 5 | 12/07/20 |
Help me pls, I don't know how to SELECT MAX ID in table1 by date in table2.
For example result should be:
for "aaa": ID 3 from table2
for "bbb": ID 5 from table2
I'm trying something like that:
DATE = (SELECT MAX(DATE) FROM table2 t2, table1 t1 WHERE t1.NAME = "aaa")
But it's not working... Have you got some idea?
You need to add an additional condition (t1.id = t2.id) for the join:
SELECT MAX(DATE)
FROM table2 t2, table1 t1
WHERE t1.NAME = "aaa"
AND t1.id = t2.id
But please - don't join with comma - use explicit JOIN syntax instead:
SELECT MAX(DATE)
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE t1.NAME = "aaa"
You can also get all max dates for all names at once using GROUP BY name:
SELECT t1.NAME, MAX(t2.DATE)
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
GROUP BY t1.NAME
I have tried to take the MAX Date using this query.
Note: In the table the date format should be (Y-m-d) and the field should be set to date in order to manipulate the operations
Table - 1(Name: test1)
Table - 2(Name: test2)
QUERY TO GET MAX FROM OTHER TABLE
SELECT MAX(dates) FROM test2 JOIN test1 WHERE test1.name='aaa'
OUTPUT
Hope so this query will solve your problem.
select ID from Table1 where date= (select max(date) from T2 where name= 'aaa'
i didn't fully got you. But I think this is how you want it.... If you had these two table as one table then it would work as charm.
Or THIS ,with Two Table like you asked for
SELECT T1.Id FROM T1 INNER JOIN T2
ON T1.Id = T2.Id
where T2.date = (select max(T2.date) from T1 where T1.name= 'aaa')

MySQL select multiple row on table 2 with two differents Id from table 1 result in one row as two differents fields

Is it possible in only one MySQL query to get the desired result (below) returned into one row ?
Where id=1 in Table 1
Table 1
|------|----------|----------|
| id | refIdOne | refIdTwo |
|------|----------|-----------
| 1 | 1 | 2 |
|------|----------|-----------
Columns "refIdOne" & "refIdTwo" refer Table 2 "id" column
Table 2
|------|------------------|
| id | text |
|------|------------------|
| 1 | cheese |
| 2 | made with milk |
|------|------------------|
Desired result returned in ONE ROW with custom AS columns named "subject" and "description" :
|----------|-----------------|
| subject | description |
|----------|-----------------|
| cheese | made with milk |
? Many thanks for help
* EDIT : Answer is *
select t21.text as subject, t22.text as description
from Table1 as t1
join Table2 as t21 on t1.refidone = t21.id
join Table2 as t22 on t1.refidtwo = t22.id
where t1.id = 1
Table2 has to joined twice to Table1.
select t21.text as subject, t22.text as description
from table1 t1
join table2 t21 on t1.refidone = t21.id
join table2 t22 on t1.refidtwo = t22.id
You need to perform Self join. Join Table2.ID twice with Table1.refIdOne and Table1.refIdTwo
select t2.text as subject, t3.text as description
from Table1 t1
Left join Table2 t2 on t1.refIdOne = t2.id
Left join Table2 t3 on t1.refIdTwo = t2.id
without joins
SELECT
(SELECT text FROM Table2 WHERE id = (SELECT refIdOne FROM Table1 WHERE id = 1) ) AS name,
(SELECT text FROM Table2 WHERE id = (SELECT refIdTwo FROM Table1 WHERE id = 1) ) AS description

update rows after left join

I'm looking for a way to update some values in a table if they were used in a left join.
I have two tables:
table1:
id | name | age | job
1 | john | 31 |
2 | eric | 25 |
table2:
id | job | inserted
1 | lawyer | 0
2 | dentist | 1
3 | cop | 0
Then I run the query:
UPDATE table1
LEFT JOIN
table2
ON table1.id = table2.id
SET table1.job = `table2.job`
WHERE table2.inserted = 0
But I want to update the rows from table2 that were used in the update so they have inserted = 1. This for two reasons 1) to speed up the join and 2) so I can check which rows of table2 were not used. (The inserts in table2 happen before table1, but the ids in table2 should always be present in table1 if all cron jobs run okay.)
You shouldn't be using a LEFT JOIN, since you only want to update rows in table1 that have a matching row in table2. Try:
UPDATE table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id
SET t1.job = t2.job, t2.inserted = 1
WHERE t2.inserted = 0
DEMO

Update table new column with values from another table (MySQL)

I have two tables table1 and table2.
I'm doing some changes and I realized that table2 is not needed, but this table has lots of data already and I need to pass the values of ID_B from table2 to table1.
Here's the structure:
table1
ID_table1 | ID_table2 | ID_B
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
table2
ID_table2 | ID_B
1 | 14
2 | 26
3 | 26
So what I want is the MySQL query to pass the ID_B value from table2 to table1 when the ID_table2 on table1 is equal to the ID_table2 on table2.
For example, the row on table1 where the ID_table1 is 1 would have the ID_B = 14.
Can you help me on this?
Thanks in advance,
Miguel.
Using JOINs you can do as.
update table1 t1
inner join
table2 t2 on t2.ID_table2 = t1.ID_table2
set t1.ID_B = t2.ID_B
DEMO
You could try it like so:
UPDATE
table1 AS target,
(SELECT ID_table2, ID_B FROM table2) AS source
SET
target.ID_B = source.ID_B
WHERE
target.ID_TABLE2 = source.ID_table2