I have two table named table_a and table_b.
table_a data
id name
1 sahadat
2 kamrul
3 Harish
4 Ilma
5 Martin
6 Ponting
table_b data
id table_a_id
1 2
2 5
3 1
Now I want to select data from table_a like this
id data
3 Harish
4 Ilma
6 Ponting
how do I write query to get this result.
what I have tried
select * from `table_a` where `id` NOT IN (select table_a_id from table_b);
It works when table_b have some data. But when table_b is empty then it show error
select * from table_a where id not in (select table_a_id from table_b);
Please use below query.
SELECT t1.* FROM table_a AS t1 WHERE NOT EXISTS
( SELECT * FROM table_b AS t2 WHERE t2.table_a_id = t1.id);
Try it
mysql_query("SELECT * FROM table_a t1 WHERE NOT EXISTS
(SELECT * FROM table_b t2 WHERE t2.table_a_id = t1.id)");
Related
I have two tables table1(userid,regid) and table2(userid,hostuserid,status) where either userid or hostuserid equals myuserid(already contained in a variable $userid).I want to find all rows in table1 such that table1.userid!=table2.userid AND table1.userid!=table2.userid.
table1
--------
userid regid
1 gbjnknnk
2 bvgcghb
3 bjbnjb
table2
-------
userid hostuserid
1 5
5 2
$userid=5
query should return only one row=> 3 bjbnjb
how to implement the same.
what i had tried
SELECT * FROM table1 JOIN table2 WHERE (table1.userid!=table2.userid AND
table2.hostuserid=$userid) AND (table1.userid!=table2.hostuserid AND
table2.userid=$userid)
You could use a not in on a subselect with union
select userid
from table1
where user1 not in (
select userid
from table2
union
select
hostuserid from table2
)
How get selected rows where user id is not in table record
the table like this
table_a
id subid userid
1 2 123
2 4 123
table_b
id title
1 like
2 liked
3 bookmark
4 bookmarked
i use join table to get the result
if WHERE userid=123 then get result
id title
2 liked
4 bookmarked
the query
select b.id,b.title from
table_a a
LEFT JOIN table_b b
ON b.id = a.subid
WHERE a.userid = '123'
but, how if userid = '345' and not in table_a get the result too?.. the result is must like this (selected rows)
id title
1 like
3 bookmark
Thankyou
You can use the following query:
SELECT id, title FROM t2 LEFT JOIN t1 ON t1.subid = t2.id
WHERE (t1.userid = 345 AND NOT userid IS NULL) OR (
NOT EXISTS(SELECT userid FROM t1 WHERE userid = 345) AND userid IS NULL
);
The working examples you can find here: http://sqlfiddle.com/#!9/c6a446/25
Try using this query
select a.no, b.title from a join b on (b.no=a.no) where a.userid='345'
You can use left join and get the data for the table where their is no match. I have solved this recently.
Chk this link:
Least Popular Event - Get list of events where bookings IS NULL OR LEAST
In mysql
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid IS NULL;
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid <> '345';
in laravel :
$least = bmodel::leftJoin('a', function($join)
{
$join->on('a.subid', '=', 'b.no');
})
->select('b.no','b.title')
->whereNull('a.userid')
->first(); or get()
You can use this query to get one or more results. It basically compares userid column with no column and gets list of no and title where there is no match in table a.
This basically would select with given userId or if that is not present it then selects the result which
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 123||
(select userid from t1 where userid =123LIMIT 1) is null
This is the second case where you will get all the rows where it is not 123 or any number
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 345 ||
(select userid from t1 where userid =345 LIMIT 1) is null
I have the following two tables:
Table 1:
student_id Name
...................
1 Kumar
2 kishan
3 Mohan
4 Kanha
5 Murat
Table 2:
student_id is_attend
.........................
1 1
4 1
Table 2 represents the students who have an attended status.
The following is the result we would like to get. So that we can show through PHP the attendance of each student.
Result:
student_id is_attend
..........................
1 1
2 0
3 0
4 1
select T1.student_id,T2.is_attend from
table1 T1 left join
table2 T2 on
T1.student_id=T2.student_id;
Use Left join as below :
Select Table1.*,Table2.is_attend
from Table1
left join Table2
on Table1.student_id = Table2.student_id
You can use sub-query also :
select s.student_id,
(select IFNULL(is_attend,0) from attend where student_id=s.student_id) as is_attend
from student as s
You can use the following query:
SELECT t1.student_id,
t1.Name,
IFNULL(t2.is_attend,0)
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.student_id = t2.student_id
You can see this here -> http://sqlfiddle.com/#!9/86511/2
Hope this helps!!!
I have 2 tables as follows:
table1
ID Name Test
A011 John 1
A012 Lynda 1
A013 Micheal 1
A014 Jack 0
A021 Joe 1
A015 Paul 0
table2
ID Done
A011 1
A012 1
I want to select all rows from table1 that have an ID where the 3 first letters are equal to A01, and the test field is 1, and also the ID is not present in table2.
I tried this query:
SELECT a.* FROM table1 a LEFT JOIN table2 b ON a.ID = b.ID
WHERE a.test = 1 AND b.ID IS NULL
The result from that is 2 rows with ID A013 and A021. I tried to use LEFT(ID,3) to get the ID with A01, however, I couldn't achieve what I want.
How can I filter only the records where the ID starts with A01?
Try this, it will give you the desired result
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.userid = t2.userid WHERE LEFT(t1.userid , 3) LIKE '%A01%' AND t1.userid NOT IN (SELECT userid from table2)
SELECT * FROM table1
WHERE test = 1
AND ID LIKE "AO1%"
AND ID NOT IN (SELECT ID from table2)
I have these two tables in my database :
table_A: table_B:
id user grade id user date
1 Mike 9.5 1 Mike 9:05
2 Dan absent 2 Dan 7:40
3 Tom 9.4 3 Tom 3:20
4 Lina 7.4 4 Lina 7:40
5 Cynthia 5:39
6 Sam 4:50
And i'm using this SQL query, to detect which users in table_B do not exist in table_A, and select those users (not existing in table A) ids:
SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
And i'm using this php script to put the ids in a variable $not:
$sql=" SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A)";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result)){
$not=$row[0];
}
The thing is, for those users not existing the query found, i want to extract their names and ids only (without date), and insert it in table_A and have an empty grade.. Any help?
Insert into table_A(user,id,grade)
SELECT table_B.id,table_B.user,'' FROM table_B
WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
You can use INSERT INTO SELECT.
https://dev.mysql.com/doc/refman/5.0/en/insert-select.html
Your query would look like:
INSERT INTO `table_A` (`id`, `user`)
SELECT `id`, `name` FROM `table_B`
WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
Try this,
Insert into table_A(id, user, '') SELECT b.id, b.user FROM table_B b WHERE b.id NOT IN (SELECT a.id FROM table_A a);