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!!!
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
)
Table 1 : Main_Family_Member
ID | Name
1 | Mahesh
2 | Rahul
3 | Jay
Table 2 : Family_Members
ID | MainMember | Name
1 | 1 | 'Arun'
2 | 1 | 'Nitin'
3 | 2 | 'Pratik'
Want Result :
Name
Mahesh
Arun
Nitin
Rahul
Pratik
You can achieve this by doing a UNION ALL of the two tables along with proper ordering. Note that it is necessary to union the two tables joined, because we need to know whether a main family member has any members. In case he does not have any member, your sample output implies that you don't want to display that main family member at all.
SELECT t.Name
FROM
(
SELECT DISTINCT t1.ID, t1.Name, 0 AS position
FROM
(
SELECT t1.ID, t1.Name
FROM Main_Family_Member t1
INNER JOIN Family_Members t2
ON t1.ID = t2.MainMember
WHERE t2.ID IS NOT NULL
) t1
UNION ALL
SELECT t2.ID, t2.Name, 1 AS position
FROM
(
SELECT t2.MainMember AS ID, t2.Name
FROM Main_Family_Member t1
INNER JOIN Family_Members t2
ON t1.ID = t2.MainMember
WHERE t2.ID IS NOT NULL
) t2
ORDER BY ID, position, Name
) t
Demo here:
SQLFiddle
SELECT Main_Family_Member.Name, Family_Members.Name
FROM Main_Family_Member
INNER JOIN Family_Members
ON Main_Family_Member.ID = Main_Family_Member.MainMember;
SELECT Main_Family_Member.Name, Family_Members.Name
FROM Main_Family_Member
INNER JOIN Family_Members
ON Main_Family_Member.ID = Main_Family_Member.MainMember;
You will need to perform a INNER JOIN on the two tables. This would return all the rows in the first table that meet the conditions plus all rows on the second table that join with the first table on the unique fields.
SELECT Main_Family_Member.Name , Family_Members.Name FROM Main_Family_Member INNER JOIN Family_Members ON Main_Family_Member.ID = Family_Members.MainMember WHERE Main_Family_Member.ID = 1 OR Main_Family_Member.ID = 2
SELECT Main_Family_Member.Name,Family_Members.Name FROM Main_Family_Member
INNER JOIN Family_Members ON Main_Family_Member.ID=Family_Members.MainMember
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 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);