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)
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 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)");
I would like to join two tables and only print records from table 1 where the rec_number is NOT in table 2.
table 1
name rec_number
john smith 123
Bob jonson 345
etc
Table 2
Bob jonson 345
etc
What is the query in php that would do this so the query only gives me John smith, not bob jonson.
is it:
$query = "select * from table1
left join rec_number on table1.rec_number = table2.rec_number";
$result=mysql_query($query);
Thank you.
You can use this query
select
t1.*
from table1 t1
left join table2 t2
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL
Beside the left join mentioned by Abhik, you could also use a subselect:
SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);