Here is my tables structure.
Table1 // it has 4 colums,that saves all the question posted by user
id|question|answer|postby|
Table2 //it has 2 colums, that saves all the signed up users
id|username|email|
Table3 //it saves that which question is read by which user.
id|reader_id|question_id|
NOTE:reader_id is the id of table 2.
Problem: We need to find out those questions that are not being read by a particular user.
select * from table1 where id not in (
select question_id from table3 where reader_id = [particular user id]
)
SELECT question FROM table1 where id NOT IN (SELECT question_id FROM table3 where reader_id IN (SELECT id FROM table2 where username='XXXX'))
Try this:
SELECT t1.*
FROM table1 t1
LEFT JOIN
(SELECT t3.*
FROM
table2 t2
JOIN table3 t3 ON t3.reader_id=t2.id
WHERE t3.reader_id=SOME USER)t ON t.question_id=t1.id
WHERE t3.id IS NULL;
Related
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;
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;
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)
I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;