Regroup two SELECT - php

This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?

If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;

Related

How to copy data from one table to another based on ids present in third table in MySQL?

I have three tables like
Table1
It's a blank table that have same columns as Table2.
Table2
It contains the actual data that needs to be copied in Table1.
id cola colb colc cold
1 hd dj dj dh
2 hf uy ug se
...
Table3
Before copying data from Table2 to Table1, first I need to verify that if the id is present in Table3 or not. In other words I just want to copy the rows from Table2 to Table1 whose id exist in Table3.
id col1 col2
1 xy zz
2 ys sh
One more thing Table2 & Table3 contains half millions of rows, so query must be feasible.
some one make this as a duplicate question
however the answer is
insert into Table1
select * from Table2 where Table1.id=Table2.id
Option 1 using subquery:
insert into Table1
select * from Table2 where id in (select id from Table3)
Option 2 using INNER JOIN:
insert into Table1
select * from Table2 INNER JOIN Table3 USING(id);
insert into Table1
select * from Table2 where id in (select id from Table3)
Above query will work if Number of columns in table1 and table2 are same
Else
insert into Table1 (column1,column2,.....)
select column1,column2... from Table2 where id in (select id from Table3)
Use join. It is faster.
insert into table1 (id, cola, colb, colc, cold)
select t2.id,t2.cola,t2.colb,t2.colc,t2.cold
from table2 t2
left join
table3 t3
on t2.id=t3.id
where t3.id is null
You can use EXISTS
Query
insert into Table1(id, cola, colb, colc, cold)
select id, cola, colb, colc, cold
from Table2 t
where exists(
select * from Table3
where id = t.id
);
OR with JOIN.
insert into id, cola, colb, colc, cold
select t1.id, t1.cola, t1.colb, t1.colc, t1.cold
from Table2 t1
join Table3 t2
on t1.id = t2.id;

Join tables on missing matched rows from the second table

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')

Select unmatched records from two tables of MYSQL

I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;

Double tables select show?

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.

How to Remove matching rows From mysql Join Result and show non-matching rows?

Is there any way to remove the matching rows from MySQL join Query.
Actually I have two tables where I have store the pub_id, and post_id in both tables these are common.
I want a result when I query all the matching rows from table1 and table2 should not be listed and the non-matching rows should be listed only.
Query return rows which exists only in one of two tables:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS (Select 1 from Table2 t2
Where t1.pub_id = t2.pub_id
AND t1.post_Id = t2.post_id)
UNION ALL
SELECT *
FROM Table2 t1
WHERE NOT EXISTS (Select 1 from Table1 t2
Where t1.pub_id = t2.pub_id
AND t1.post_Id = t2.post_id)
you need something like that:
SELECT * FROM tablea AS a
RIGHT JOIN tableb AS o ON a.id = o.id WHERE a.pub_id IS NULL and a.post_id is null
UNION
SELECT * FROM tablea AS a
LEFT JOIN tableb AS o ON a.id = o.id WHERE o.pub_id IS NULL and o.post_id is null

Categories