SELECT Name,Checktime
FROM Userinfo
LEFT JOIN Checkinout ON USERINFO.USERID = CHECKINOUT.USERID
WHERE Checktime >= #11/14/18 04:35:00 AM#;
The above query has to display all name from userinfo table but it only displays matching data with checkinout table.
In most databases, you would move the condition to the ON clause:
SELECT ui.Name, cio.Checktime
FROM Userinfo as ui LEFT JOIN
Checkinout as cio
ON ui.USERID = cio.USERID AND
cio.Checktime >= #11/14/18 04:35:00 AM#;
Alas, this standard syntax is not supported in MS Access. You can use a subquery instead:
SELECT ui.Name, cio.Checktime
FROM Userinfo as ui LEFT JOIN
(SELECT cio.*
FROM Checkinout as cio
WHERE cio.Checktime >= #11/14/18 04:35:00 AM#
) as cio
ON ui.USERID = cio.USERID ;
Your question title says LEFT JOIN not working but in your query, you are using RIGHT JOIN?
Just use LEFT JOIN in your query. It will list all users from USERINFO but only matching records from CHECKINOUT.
SELECT `Name`, `Checktime`
FROM `Userinfo`
LEFT JOIN `Checkinout` ON `USERINFO`.`USERID` = `CHECKINOUT`.`USERID`
Where `Checktime` >= #11/14/18 04:35:00 AM#;
You can set alias on each table
SELECT a.*,b.*
FROM Userinfo a
LEFT JOIN Checkinout b ON a.USERID = b.USERID
Where b.Checktime>=#11/14/18 04:35:00 AM#;
If you want to display all name from the userinfo and display the match data from the checkinout table you just need to change RIGHT JOIN to LEFT JOIN.
SELECT Checkinout.USERID, Userinfo.Name, Userinfo.Checktime,
FROM Userinfo
LEFT JOIN Checkinout ON USERINFO.USERID = CHECKINOUT.USERID
WHERE Checktime >= #11/14/18;
Try this one.
Related
I have to write a query such that ,I need to get events whose start date is of 30 min from now.
My conditions are:
1) get the event from events table
2)Join created by of events with id in users table.
3)Comments from comment table with user ser id
But the problem here is if there is no comment for event then the event it self is not coming.If any comment is present it is coming.I dont want this.If comment is not there just fetch it as empty but not hide the total event .Can anyone please help me,.Thanks.
select u.email ,group_members.user_id,users.first_name,u.first_name
as host_name,events.name,events.start_date,comments.comments,c.first_name as
comment_user,comments.id from events
inner join users as u on u.id = events.created_by
inner join comments on events.id = comments.event_id
inner join group_members on events.group_id = group_members.group_id
inner join users as c on comments.from_user = c.id
inner join users on group_members.user_id = users.id
where events.start_date between date_add(now(),interval 1 minute) and date_add(
now(),interval 30 minute)
and group_members.user_status = 2
and events.status = 2
You need a left join to the comments table. I would put that table last in the from clause.
select u.email, gm.user_id, gu.first_name, u.first_name as host_name,
e.name, e.start_date, c.comments, uc.first_name as comment_user,
c.id
from events e inner join
users u
on u.id = e.created_by inner join
group_members gm
on e.events.group_id = gm.group_id inner join
users gu
on gm.user_id = gu.id left join
comments c
on e.id = c.event_id left join
users uc
on c.from_user = uc.id
where e.start_date between date_add(now(),interval 1 minute) and date_add(now(),interval 30 minute) and
gm.user_status = 2 and
e.status = 2;
Once you use a left join on comments, you also need a left join for the from user. I replaced all table names with aliases -- this makes it easier to track which table is used for which purpose.
Use the INNER JOIN Keyword and select the two columns by putting them with keyword ON.
SELECT EMP.EMP_ID, EMP.EMP_NAME, DEPT.DEPT_NAME FROM EMP
INNER JOIN DEPT ON DEPT.DEPT_ID = EMP.DEPT_ID;
I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I am using the Sql query below:
$query = mysql_query("SELECT * FROM `venues`
LEFT JOIN `follows` USING (venue_id)
LEFT JOIN `stats` USING (venue_id, user_id)
WHERE follows.user_id = $userid");
The problem is that it's not showing some fields in the stats table.
So what I would thinking the problem might be (might be wrong), is that I need to select all the fields of that table?
If this is the case, is there a way of telling it to select * the fields for the 3 tables?
For example:
SELECT * FROM `venues`, SELECT * FROM `follows`, SELECT * FROM `stats` LEFT JOIN ....
use alias for each table then
select A., B., C.* from venues as A left join Follows as B.....
You can use alias for tables. Here's your request using those aliases:
$query = mysql_query("SELECT a.*, b.*, c.* FROM `venues` as b
LEFT JOIN `follows` as b USING (venue_id)
LEFT JOIN `stats` as c USING (venue_id, user_id)
WHERE follows.user_id = $userid");
I am currently using this script below.
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
WHERE table2.user_id = $userid");
The tables have these fields:
Table1:
id, venue_id, user_id...
Table2:
id, venue_id, user_id...
The query above returns 5 records.
Now....
I need to add a third table to the above script Table3
Table 3 fields also contains id, venue_id, user_id... BUT I don't what it in the WHERE of the script.
I've tried adding a LEFT JOIN to the script above to add the third table like this:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The Stats table only contains 1 record.
Now, my problem is that the query above it's echoing the data on ALL the records and not just the one.
My question is...What I'm I doing wrong on the line I added:
LEFT JOIN stats as table3 on table1.venue_id = table3.venue_id ?
OK, I think you want to also join on the user_id. So
SELECT
*
FROM
`venues` AS table1
LEFT JOIN `follows` AS table2 USING (venue_id)
LEFT JOIN `stats` AS table3 USING (venue_id, user_id)
WHERE
table2.user_id = $userid
is my solution
If you only want to include record from table1 that have non null records in table3 then you need to use INNER JOIN and not a LEFT JOIN. See the MySQL documentation for JOIN
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
INNER JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The "INNER" is not needed explicitly. Joins are INNER joins by default
You're not limiting the records at all. Use this instead:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid AND table3.venue_id IS NOT NULL");
You can use an INNER JOIN rather than an LEFT JOIN. See this SO post to make it clear or this blog article
I have 2 different tables: tbl_records, tbl_users
I want to fetch all data from tbl_records, which is published by f_name='Kr' and which are inserted today (date of record).
This returns all columns from tbl_records for the user with the f_name 'Kr', with date_of_record on or later than midnight today.
SELECT r.* FROM tbl_records r
INNER JOIN tbl_users u ON r.user_id = u.user_id
WHERE r.date_of_record >= DATE(NOW()) AND u.f_name LIKE 'Kr'
Though it's usually better to specify the exact columns you want, in case the table definition is changed later:
SELECT r.user_id, r.status, r.date_of_record FROM tbl_records r
INNER JOIN tbl_users u ON r.user_id = u.user_id
WHERE r.date_of_record >= DATE(NOW()) AND u.f_name LIKE 'Kr'
Try this,
SELECT * FROM tbl_records AS tr JOIN tbl_users AS tu ON ( tr.user_id = tu.user_id ) AND tr.date_of_record = DATE(NOW()) AND tu.f_name = 'kr'