I have following Mysql table :
1) products
2) product_images
3) product_date_time
4) product_menu
5) chef_profile
I need to show all data from those 5 table to a page called menu-details.php
All table have foreign key as p_id of products table id and Some table have multiple data of same p_id.
To get this done I am using following mysql join query :
$query = mysqli_query($conn, "SELECT p.p_id, p.p_title, p.p_description,
p.p_price, p.p_availability, p.delivery_option, p.event_location,
pimg.p_large_image, pimg.p_small_image, pdt.date, pm.p_menu_title,
pm.p_menu_description, pm.p_menu_price, pm.p_menu_availability, chef.fname,
chef.lname, chef.chef_details FROM products as p LEFT JOIN product_images as
pimg ON p.p_id = pimg.p_id LEFT JOIN product_date_time as pdt ON pdt.p_id =
p.p_id LEFT JOIN product_menu as pm ON pm.p_id = p.p_id LEFT JOIN
chef_profile AS chef ON chef.u_id = p.u_id WHERE p.p_id = '$mid' ") or
die('wrong query');
To show data I am using following php code :
$result = mysqli_fetch_array($query);
$menu_title = htmlspecialchars($result['p_title']);
My Questions
Should I use 5 unique sql query to show data to that menu_details.php page ?
Now this query is showing single record from those table which have multiple data of same p_id. I need to show all data which have same p_id with multiple data. How can I do this ?
For example : I have following data in product_images table :
pimg_id p_large_image p_id u_id
1 name 203 25
1 name 204 26
1 name 204 25
1 name 205 27
1 name 205 28
So If I use php while loop it's showing all data from that table but it's should be show all data based on p_id for e.g p_id = 204
while($result = mysqli_fetch_array($query)) {
echo $menu_l_image = htmlspecialchars($result['p_large_image']);
echo '<br/>';
}
If the p.id is an integer remove the single quote form $mid
"SELECT
p.p_id,
p.p_title,
p.p_description,
p.p_price,
p.p_availability,
p.delivery_option,
p.event_location,
pimg.p_large_image,
pimg.p_small_image,
pdt.date,
pm.p_menu_title,
pm.p_menu_description,
pm.p_menu_price,
pm.p_menu_availability,
chef.fname,
chef.lname,
chef.chef_details
FROM products as p
LEFT JOIN product_images as pimg ON p.p_id = pimg.p_id
LEFT JOIN product_date_time as pdt ON pdt.p_id = p.p_id
LEFT JOIN product_menu as pm ON pm.p_id = p.p_id
LEFT JOIN chef_profile AS chef ON chef.u_id = p.u_id
WHERE p.p_id = $mid "
of previus a proper sanitize of the $mid vars you can use
"SELECT
p.p_id,
p.p_title,
p.p_description,
p.p_price,
p.p_availability,
p.delivery_option,
p.event_location,
pimg.p_large_image,
pimg.p_small_image,
pdt.date,
pm.p_menu_title,
pm.p_menu_description,
pm.p_menu_price,
pm.p_menu_availability,
chef.fname,
chef.lname,
chef.chef_details
FROM products as p
LEFT JOIN product_images as pimg ON p.p_id = pimg.p_id
LEFT JOIN product_date_time as pdt ON pdt.p_id = p.p_id
LEFT JOIN product_menu as pm ON pm.p_id = p.p_id
LEFT JOIN chef_profile AS chef ON chef.u_id = p.u_id
WHERE p.p_id = ". $mid . ";"
Related
I have Quiz table which is shown in image.
and quiz result history table like
I write left join query between these two table.
My query is like:
select q.*
, c.name as catname
, qrh.q_complete
, q s.test_type
, qs.questionid
, sum(qrh.score) as score1
, sum(qrh.total_questions) as tot_que
from categories c
, quiz q
left
join user_quiz_results_history qrh
on qrh.quiz_id = q.quizid
left
join quizquestions qq
on qq.quizid = q.quizid
left
join questions qs
on qs.subcatid = qq.subjectid
where c.catid = q.catid
AND q.catid = 1
AND q.status = 'Active'
AND q.enddate >= '2016-05-02'
group
by q.quizid
This query give result all quiz table data and related user_quiz_results table.
If I write query using condition for left join table like userid=90, I don't get the any of data.
I want all data of quiz and if table have result for userid=90 get data from table user_quiz_history.avg(score).
Write condition on left join clause.
select q.* , qrh.q_complete , sum(qrh.score) as score1 , sum(qrh.total_questions) as tot_que from categories c , quiz q left join user_quiz_results_history qrh on qrh.quiz_id = q.quizid AND qrh.userid=84 where q.catid = 1 AND q.status = 'Active' AND q.enddate >= '2016-05-02' group by q.quizid
I have following 2 tables in mysql database :
1) products
p_id p_title p_price u_id
----------------------------------
1 tile 123 25
2 tile 133 24
3 tile 143 25
2) product_images
id img_name p_id u_id
1 name 1 25
2 name 1 25
3 name 2 24
I want to get all row from products table and only one row from product_images table. So to get this I am using following sql query but it's return all rows from product_images table. Should be one !
My Query :
$get_menu = mysqli_query($conn,
"SELECT DISTINCT products.p_title, products.p_price,
product_images.p_list_image, chef_profile.live_at, chef_profile.fname,
chef_profile.lname FROM products LEFT JOIN product_images ON products.p_id
= product_images.p_id LEFT JOIN chef_profile ON products.u_id =
chef_profile.u_id GROUP BY product_images.p_list_image") or die('sorry');
I am using this query in php while loop so that I want to show all unique data from products table and only one image from product_images table.
$num_menu = mysqli_num_rows($get_menu);
while( $get_result = mysqli_fetch_array($get_menu) ) {
/*echo '<pre>';
print_r($get_result);*/
$menu_title = htmlspecialchars($get_result['p_title']);
$menu_price = htmlspecialchars($get_result['p_price']);
$menu_image = htmlspecialchars($get_result['p_list_image']) ? htmlspecialchars($get_result['p_list_image']) : "no-menu-preview.png";
$live_at = htmlspecialchars($get_result['live_at']);
$fname = htmlspecialchars($get_result['fname']);
$lname = htmlspecialchars($get_result['lname']);
// echo..........data.. her...
}
There you have it.
You only have to group by p_id to get 1 result for each product.
SELECT DISTINCT products.p_title, products.p_price, product_images.p_list_image, chef_profile.live_at, chef_profile.fname, chef_profile.lname
FROM products
LEFT JOIN product_images ON products.p_id = product_images.p_id
LEFT JOIN chef_profile ON products.u_id = chef_profile.u_id
GROUP BY products.p_id
Hope it helps ;)
SELECT products.p_title,
products.p_price,
product_images.p_list_image,
chef_profile.live_at,
chef_profile.fname,
chef_profile.lname
FROM products
LEFT JOIN product_images ON products.p_id = product_images.p_id
LEFT JOIN chef_profile ON products.u_id = chef_profile.u_id
GROUP BY products.p_id,product_images.p_list_image
I am working on a project with PHP and MySQL. I am trying to run a SQL query where I need to fetch data from 5 tables. I'm getting an error on my query.
$value = json_decode(file_get_contents('php://input'));
$estId = $value->estId;
$sql = 'SELECT establishments.id, establishments.name,
establishments.stay_value, establishments.latitude,
establishments.longitude, establishments.description,
establishments.address,
facilities.id, facilities.name,
accomodations.id,accomodations.name
FROM establishments
INNER JOIN (establishments_facilities
INNER JOIN facilities
ON establishments_facilty.facility_id = facilities.id)
ON establishments.id = establishments_facility.id
INNER JOIN (establishments_accommodations
INNER JOIN accommodations
ON establishments_accommodations.accommodation_d = accomodations.id)
WHERE establishments.id ="'.$estId .'" ';
$result = $conn->query($sql);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
$json_obj = $row;
}
$json_obj['success'] = true;
echo json_encode($json_obj);
}
My 5 tables are:
establishments
1 id
2 user_id
3 name
4 logo
5 description
6 email
7 latitude
8 longitude
9 stay_value
accomodations
1 id
2 name
facilities
1 id
2 name
establishments_accommodations
1 id
2 establishment_id
3 accommodation_id
establishments_facilities
1 id
2 establishment_id
3 facility_id
Any help will be appreciated.
The query you wrote is hard to read and you should use alias for better readability and also join syntax does not look correct, here is the query with proper alias
SELECT
e.id,
e.name,
e.stay_value,
e.latitude,
e.longitude,
e.description,
e.address,
f.id,
f.name,
a.id,
a.name
FROM establishments e
INNER JOIN establishments_facilities ef on e.id = ef.id
INNER JOIN facilities f ON ef.facility_id = f.id
INNER JOIN establishments_accommodations ea on ea.establishment_id = ef.establishment_id
INNER JOIN accommodations a ON ea.accommodation_d = a.id
WHERE e.id ={some value}
Now in PHP you may have something as
$sql = "
SELECT
e.id,
e.name,
e.stay_value,
e.latitude,
e.longitude,
e.description,
e.address,
f.id,
f.name,
a.id,
a.name
FROM establishments e
INNER JOIN establishments_facilities ef on e.id = ef.id
INNER JOIN facilities f ON ef.facility_id = f.id
INNER JOIN establishments_accommodations ea on ea.establishment_id = ef.establishment_id
INNER JOIN accommodations a ON ea.accommodation_d = a.id
where e.id = '".$estId."'";
Note that where e.id = '".$estId."'" is vulnerable to sq-injection, so better to use prepared statement.
want to combine this 4 query in single mysql query and my current query is
$sql1 = "SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3"
$sql2 = "SELECT * FROM projectFiles pf
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
WHERE pp.projectID = '".$row['projectID']."'"
$sql3 = "SELECT * FROM projectFiles pf
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
WHERE pm.projectID = '".$row['projectID']."' "
$sql4 = "SELECT * FROM projectFiles pf
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE ps.projectID = '".$row['projectID']."' "
If I understand your requirement correctly, you have four tables, and you want to include any records from those tables whose projectID matches the one in your provided $row['projectID'] field. If so, you can simply combine all your queries like this:
$sqlWhatever =
"SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3
OR pp.projectID = '".$row['projectID']."'
OR pm.projectID = '".$row['projectID']."'
OR ps.projectID = '".$row['projectID']."' "
Now, you haven't said whether each of the four tables has the same fileID's or not. If they do not, then change your INNER join to a LEFT join, or you'll only get records that have a common fileID in each of the four tables.
I have several tables I'm joining to display my product details from a stock table. Therefore, one product ID can have many entries in the stock table due to multiple colours and sizes.
Table 1: tblStock
stockID
productID
sizeID
colourID
qty
Table 2: tblColour
colourID
colourName
colourHEX
Table 3: tblSize
sizeID
sizeName
My query:
SELECT p.productID, c.colourName, c.colourHEX, sz.sizeName, s.qty
FROM tblProducts p
INNER JOIN tblStock s ON p.productID = s.productID
INNER JOIN tblColour c ON s.colourID = c.colourID
INNER JOIN tblSize sz ON s.sizeID = sz.sizeID
WHERE p.productID = '$id'
is returning:
productID colourName colourHEX sizeName qty
4 Burgundy #621b40 Small 10
4 Burgundy #621b40 Medium 15
4 Burgundy #621b40 Large 20
4 Pink #ba0046 Large 20
Is there any way that I can only return the product ID once but keep all it's variations?
If not, how would I echo this product to the page as I don't want it to appear 4 times. Just appear once with the various options available detailed.
You can use the group_concat function
SELECT p.productID, group_concat(distinct c.colourName), group_concat(distinct c.colourHEX), group_concat(distinct sz.sizeName), sum(s.qty)
FROM tblProducts p
INNER JOIN tblStock s ON p.productID = s.productID
INNER JOIN tblColour c ON s.colourID = c.colourID
INNER JOIN tblSize sz ON s.sizeID = sz.sizeID
WHERE p.productID = '$id'
group by p.productID ;// group by required only if you product id clause is removed
I'm not used to PHP but your problem is very simple.
All you need is a separate variable inside a loop like this:
sql="SELECT p.productID, c.colourName, c.colourHEX, sz.sizeName, s.qty"
sql=sql+"FROM tblProducts p"
sql=sql+"INNER JOIN tblStock s ON p.productID = s.productID"
sql=sql+"INNER JOIN tblColour c ON s.colourID = c.colourID"
sql=sql+"INNER JOIN tblSize sz ON s.sizeID = sz.sizeID"
sql=sql+"WHERE p.productID = '$id'"
Set rs = RecordSetForStoringSQLQueryResult -- Replace with appropriate code in PHP
rs.Open (connectionName, sql)
$prevProductID = Null; -- Or some other number
while not rs.EndOfFile
$currentId = rs("productID")
if ($currentId != $lastId) {
-- New ID found, display it.
echo($currentId);
$prevProductID = $currentId
} else {
-- Code for logic when the current product ID is same as previous product ID
-- You will not assign anything to $prevProductID in this section.
}
rs.MoveToNextRecord
end while
Kindly consider that I am not a PHP guy, but I have done similar things in classic ASP. The code above is just for illustration purposes and it is not directly usable. You will need to tweak it as per your plan.