MySQL join multiple tables error - php

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`

Related

Left JOIN query not displaying data

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.

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

full outer join sql

I created 2 simple table. tableA and table B, both only have 1 column 'po'.
I am trying to combine the 2 table together using FULL OUTER JOIN.
The error output is Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result.
So I guess pretty much my query is wrong. Appreciate any help here!
<?php
include 'db.php';
$sqlcount="SELECT DISTINCT po FROM testonly FULL OUTER JOIN testonly2 ON testonly.po=testonly2.po";
$resultcount = mysqli_query($conn,$sqlcount);
while($row = mysqli_fetch_array($resultcount)){
echo $row['po'];
}
?>
There is no FULL OUTER JOIN built in with MySQL. But, you could simulate your query via:
SELECT * FROM testonly t1
LEFT JOIN testonly2 t2 ON t1.po = t2.po
UNION -- use UNION ALL to retain common rows
SELECT * FROM testonly t1
RIGHT JOIN testonly2 t2 ON t1.po = t2.po
PHP code:
$sqlcount = "SELECT * FROM testonly t1 LEFT JOIN testonly2 t2 ON t1.po = t2.po UNION ALL SELECT * FROM testonly t1 RIGHT JOIN testonly2 t2 ON t1.po = t2.po";
Read here for more information: Full Outer Join in MySQL
if you need an united table you need and union You can't use FULL OUTER JOIN because this is not available in mysql ..but yuo can emulate with and an union select
The union clause implies that only distint value are merged for the two table ..
$sqlcount="SELECT po
FROM testonly
UNION
testonly2 ";
the union all implies the select of all the rows (also duplicated) from both the tables
$sqlcount="SELECT po
FROM testonly
UNION ALL
testonly2 ";
otherwise if you need a real join (bothe the column on the same rows ) you can use JOIN (inner join .. or left join, or right ) depending the way you need join the table
this is for inner join
$sqlcount="SELECT DISTINCT t1.po, t2.po
FROM testonly t1 INNER JOIN testonly2 t2 ON testonly.po=testonly2.po";

retrieving values from multiple tables in mysql

I am using php and I have to get the data from multiple tables with common id, but the problem is that in few tables that common id contains multiple records,using inner join gives me separate rows of data e.g.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"2"}
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"3"}
extraId is the multiple record for the dish_id:52.
I need result like this.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId"[{"id":"2"},{"id":"3"]}}
My query is:
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status'";
please help.
Use group by and group_concat. Something like this:
Select d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type,
group_concat(e.extra_id) as extraids
from order_main m INNER JOIN
order_detail d
ON m.id = d.order_id INNER JOIN
order_extras e
ON m.id = e.order_id INNER JOIN
order_addons a
ON m.id = a.order_id
where m.franchise_id = '$storeId' and d.STATUS <> '$order_status'
group by d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type;
Your desired results do not include these columns:
e.extra_title
a.addon_id
a.addon_size
I would also suggest that you remove the join to order_addons.
Notice that table aliases make the query easier to write and to read.
You can use group bywith dish_id
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status' group by order_detail.dish_id";

PHP SQL Adding Multiple Selects

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");

Categories