retrieving values from multiple tables in mysql - php

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

Related

MySQLi inner join not working properly

I have a query that select shows alongside with it's rating. But it will not work if there isn't any rates. I want it to work even when it finds zero results on the rating table.
My query is
$shows = $DB->query('SELECT
p.id, p.title, p.cover, p.summary, p.genre, p.year,
ROUND(AVG(pr.rating), 1) AS rating_average
FROM shows p
INNER JOIN shows_ratings pr
ON pr.showid = p.id');
Change inner join with left join.
Explanation: You are trying to join two table on key which doesn't exist in other table, that's why you are not getting any result in inner join. Whereas left join will return rating as empty when key is not present in rating table.
$shows = $DB->query('SELECT
p.id, p.title, p.cover, p.summary, p.genre, p.year,
ROUND(AVG(pr.rating), 1) AS rating_average
FROM shows p
LEFT JOIN shows_ratings pr
ON pr.showid = p.id');

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

how to use INNER JOIN and IN Clause in same query

i have three tables and want to run INNER JOIN and IN clause on them.
can anyone tell me where i am doing wrong
SELECT `tblinvoices`.id,`tblinvoices`.userid,`firstname`,`lastname`
FROM `tblinvoices`
WHERE `paymentmethod`IN
(SELECT `gateway` FROM `tblpaymentgateways` WHERE `setting`='type' AND `value` = 'CC')
INNER JOIN `tblclients` ON `tblinvoices`.userid=`tblclients`.id"
JOIN comes before WHERE:
SELECT tblinvoices.id,
tblinvoices.userid,
firstname,
lastname
FROM
tblinvoices
INNER JOIN tblclients
ON tblinvoices.userid = tblclients.id
WHERE
paymentmethod IN
(select gateway
FROM tblpaymentgateways
WHERE setting='type'
AND value = 'CC')

MySQL join multiple tables error

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`

mysql join return 0 for one column with other columns intact

I wish to join multiple tables like- Categories, menus, restaurants, reviews, etc.
to return the restaurants that provide the inserted food with their prices.
Everything works except numberOfReviews in reviews table.
If a restaurant has no reviews then output should be 0 for numOfReviews column but other column values should be retrieved i.e. price, name, etc.
With following query I get all fields as null and count(numReviews) as 0:
select r.id
,r.`Name`
,r.`Address`
,r.city
,r.`Rating`
,r.`Latitude`
,a.`AreaName`
,m.`Price`
,count(rv.id)
from `categories` c, `menus` m, `restaurants` r, areas a, reviews rv
where m.`ItemName`="tiramisu"
and c.`restaurant_id`=r.`id`
and m.`category_id`=c.id
and r.`AreaId`=a.`AreaId`
and if I can't match rv.restaurant_id=r.id in where clause(obviously).
Where am I getting wrong? How do I solve this?
edited
select r.id,
r.`Name`,
r.`Address`,
r.city,
r.`Rating`,
r.`Latitude`,
a.`AreaName`,
m.`Price`,
r.`Longitude`,
r.Veg_NonVeg,
count(rv.id)
from restaurants r LEFT JOIN `reviews` rv on rv.`restaurant_id`=r.`id`
inner join `categories` c on c.`restaurant_id` = r.id
inner join `menus` m on m.`category_id` = c.id
inner join `areas` a on a.`AreaId` = r.`AreaId`
where m.`ItemName`="tiramisu"
First of all, don't use this old school syntax for the jointures.
Here is a query that may solve your problem:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,COUNT(RV.id) AS numOfReviews
FROM restaurants R
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
LEFT JOIN reviews RV ON RV.restaurant_id = R.id
WHERE M.ItemName = 'tiramisu'
GROUP BY R.id, R.Name, R.Address, R.city, R.Rating, R.Latitude, R.Longitude, A.AreaName, M.Price, R.Veg_NonVeg
I used explicit INNER JOIN syntax instead of your old school syntax and I modified the jointure with table reviews in order to get the expected result. The GROUP BY clause is required to use the aggregate function COUNT, every rows will be grouped by the enumerated columns (every column except the one used by the function).
Here is another solution that simplify the GROUP BY clause and allow the modification of SELECT statement without having to worry about the fact that every columns need to be part of the GROUP BY clause:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,NR.numOfReviews
FROM restaurants R
INNER JOIN (SELECT R2.id
,COUNT(RV.id) AS numOfReviews
FROM restaurants R2
LEFT OUTER JOIN reviews RV ON RV.restaurant_id = R2.id
GROUP BY R2.id) NR ON NR.id = R.id
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
WHERE M.ItemName = 'tiramisu'
As you can see here I added a new jointure on a simple subquery that does the aggregation job in order to provide me the expected number of reviews for each restaurant.
Hope this will help you.

Categories