I am stuck on a query where I want to display data from different tables and their bridge tables.
The tables are:
bridge (bid,planid,fbridgeid)
fbridge (fbridgeid,featureid,valueid)
features (featureid,fname)
value (valueid,value)
plans (planid,name,price,discount)
Below is my query so far:
SELECT
(NAME AS pname, price AS prize,
discount AS ds, fname AS feature, VALUE AS vlue
FROM plans )
I want to get all the columns from database , but according to the plans
like planid 1 ->> contains ->> feature id 1,2,3 and valueid 1,3,5 etc )
The Joins will be:
SELECT pl.name AS pname, pl.price AS prize , pl.discount AS ds , fe.fname AS feature, va.VALUE AS vlue
FROM plans pl
JOIN bridge br ON br.planid = pl.planid
JOIN fbridge fb ON fb.fbridgeid = br.fbridgeid
JOIN features fe ON fe.featureid = fb.featureid
JOIN value va ON va.valueid = fb.valueid
WHERE pl.plainid = 1
Join your tables on their correct fields.
SELECT *
FROM plans p
INNER JOIN bridge b ON p.planid = b.planid
INNER JOIN fbridge fb ON b.fbridgeid = fb.fbridgeid
INNER JOIN features f ON fb.featureid = f.featureid
INNER JOIN value v ON fb.valueid = v.valueid
Then you can select any fields from them all.
SELECT p.name AS pname, p.price AS prize, p.discount AS ds, f.fname AS feature, v.value AS vlue
FROM plans p
INNER JOIN bridge b ON p.planid = b.planid
INNER JOIN fbridge fb ON b.fbridgeid = fb.fbridgeid
INNER JOIN features f ON fb.featureid = f.featureid
INNER JOIN value v ON fb.valueid = v.valueid
select b.bid,b.planid,b.fbridgeid,fb.fbridgeid,f.featureid,f.fname,v.valueid,v.value,p.name,p.price,p.discount from bridge as b
inner join fbridge as fb on fb.bridgeid=b.bridgeid
inner join features as f on f.featureId=fb.featureid
inner join value as v on v.valueid=fb.valueid
inner join plans as p on p.planid=b.planid
where p.planid=1 and f.featureId in (1,2,3) and v.valueid in (1,3,5)
Related
I have this EAV data model
products(prod_id, prod_name,...)
attribute_key(attr_key_id, attr_name)
attributes_values(attr_val_id, attr_key_id, value)
product_attr_values(id,product_id,attr_val_id)
I having a product like a book, which can have multiple attribute like Author(can be multiple author for a book), Editor(can be multiple), publish year, Publisher etc.... There will be a search based on author in which i'm using below query and attached screenshot of query result.
I am using the query below:
SELECT p.prodname, f.attribute_name, v.value FROM products_ecomc AS p LEFT
JOIN product_attribute_value AS t ON p.prodid = t.product_id
LEFT JOIN attribute_value_ecomc AS v ON t.attribute_value_id =
v.attr_value_id
LEFT JOIN attr_common_flds_ecomc AS f ON v.attr_id = f.id
WHERE
t.product_id IN
(
SELECT
t.product_id
FROM
products_ecomc p
LEFT JOIN product_attribute_value t ON p.prodid = t.product_id
LEFT JOIN attribute_value_ecomc v ON t.attribute_value_id = v.attr_value_id
LEFT JOIN attr_common_flds_ecomc f ON v.attr_id = f.id
WHERE
((f.attribute_name="Author" AND v.value="sabsyasachi das")
OR
(f.attribute_name="Author" AND v.value="Ela Datta"))
AND
(p.subcatid IN (5,6,7,8,9,10)) GROUP BY t.product_id
);
Please find my query result here
I want the result like below:
prodname |Aurhor1 | Aurhor2 |Editor1 |Editor2 |Place
--------------|---------------|---------|----------|------------------|-------
Ramkinkar Baij|sabsyasachi das|Ela Datta|Sathi Basu|Radha Prasad Gupta|Panagarh
Please find sqlfiddle here
Thanks in advance please find a solution
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.
I've got repeating records with different 'manufacturer' fields. I'm trying to GROUP BY i.stocknumber but that only removes a record without collecting the other manufacturer result.
Some records have a NULL manufacturer field, and I've tried using GROUP_CONCAT in the subquery to elide the differing results for "manufacturer".
Here's my current query:
SELECT i.id,i.stocknumber,m.manufacturer
FROM inventory i
INNER JOIN makes mk on i.make = mk.id
INNER JOIN models md on i.model = md.id
INNER JOIN classes cl on i.class = cl.id
LEFT JOIN (
SELECT idm.id, idm.inventory_id, dm.manufacturer AS 'manufacturer'
FROM display_manufacturers dm
INNER JOIN inventory_display_manufacturers idm ON dm.id = idm.display_manufacturer_id
) m ON i.id = m.inventory_id
-- GROUP BY i.stocknumber
ORDER BY i.stocknumber
The result I get is:
Any thoughts on grouping the repeated records by concatenating the manufacturer field?
(NOTE: I already tried GROUP_CONCAT on the subquery)
DESIRED RESULT:
id | stocknumber | manufacturer
----------------------------------
946 | 011A | NULL
907 | 1001 | Sports Coach, Coachmen
1032 | 1001x | Sports Coach
etc....
Are you looking for this?
SELECT i.id, i.stocknumber, group_concat(distinct m.manufacturer) as manufacturers
. . .
GROUP BY i.stocknumber
EDIT:
This version explicitly uses your query as a subquery. It should not put all manufacturers on the same row:
select stocknumber, group_concat(distinct manufacturer) as manufacturers
from (SELECT i.id,i.stocknumber,m.manufacturer
FROM inventory i
INNER JOIN makes mk on i.make = mk.id
INNER JOIN models md on i.model = md.id
INNER JOIN classes cl on i.class = cl.id
LEFT JOIN (
SELECT idm.id, idm.inventory_id, dm.manufacturer AS 'manufacturer'
FROM display_manufacturers dm
INNER JOIN inventory_display_manufacturers idm ON dm.id = idm.display_manufacturer_id
) m ON i.id = m.inventory_id
) t
GROUP BY stocknumber ;
If what you are trying to do is removing the NULL fields, you just have to add a WHERE condition excluding the NULL fields
SELECT i.id,i.stocknumber,m.manufacturer
FROM inventory i
INNER JOIN makes mk on i.make = mk.id
INNER JOIN models md on i.model = md.id
INNER JOIN classes cl on i.class = cl.id
LEFT JOIN (
SELECT idm.id, idm.inventory_id, dm.manufacturer AS 'manufacturer'
FROM display_manufacturers dm
INNER JOIN inventory_display_manufacturers idm ON dm.id = idm.display_manufacturer_id
) m ON i.id = m.inventory_id
WHERE m.manufacturer IS NOT NULL
-- GROUP BY i.stocknumber
ORDER BY i.stocknumber
I have 3 tables.
First table keeps "group_names" with id numbers. Second table keeps "groups_elements" with id numbers and group_id numbers next to element_name. Third table keeps relations between group_elements which includes element_id, sub_element_id.
I wish to get concat group_name, element_name and element_id numbers sub_elements numbers.
Here is sqlfiddler link http://sqlfiddle.com/#!2/44f63
And i wish to get such result:
Solid Soil 5,6,7
Liquid Oil 8,9,10
I am using MySQL and PHP.
You can do so
SELECT CONCAT(g.group_name,' , ',e.element_name)
, GROUP_CONCAT(DISTINCT er.sub_element_id)
FROM groups g
JOIN elements e ON(g.id = e.group_id)
JOIN element_subelement_relation er ON(er.element_id= e.id)
GROUP BY g.group_name, e.element_name
Demo
Edit from comments
SELECT CONCAT(g.group_name,' , ',e.element_name) `group_elements`
, GROUP_CONCAT(DISTINCT er.`sub_element_id`) `ids`
FROM groups g
LEFT JOIN elements e ON(g.id = e.group_id)
LEFT JOIN element_subelement_relation er ON(er.element_id= e.id)
GROUP BY g.group_name, e.element_name
HAVING group_elements IS NOT NULL
ORDER BY g.group_name
Demo
using group_concat()
SELECT g.group_name, group_concat(sub_element_id) as items
FROM elements e INNER JOIN element_subelement_relation er
ON e.id = er.element_id INNER JOIN groups g
ON g.id = e.group_id
GROUP BY g.group_name
demo: http://sqlfiddle.com/#!2/44f63/21
Simply join and use group_concat on the sub ids:
select
concat(g.group_name, ' ', e.element_name) as name,
group_concat(sub_element_id order by sub_element_id) as sub_elements
from elements e
inner join groups g on g.id = e.group_id
inner join element_subelement_relation r on r.element_id = e.id
group by name
order by sub_elements, name;
The SQL fiddle: http://sqlfiddle.com/#!2/44f63/31.
You may also want to try these:
SELECT g.group_name, e.element_name, concat(g.id,",", e.id,",",esr.id) as ID
FROM element_subelement_relation esr
LEFT JOIN elements e ON(esr.element_id = e.id)
LEFT JOIN groups g ON(e.group_id = g.id)
I have table name students having fields student_id, house_id etc..
and subjects, houses, students_subjects
I am using this query
SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id WHERE students_subjects.class_years_section_id=1
This is working fine for me ..
Now i want to get house name too from houses table
I tried this query
SELECT students_subjects.student_id,students.house_id,houses.house_name, students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id
LEFT JOIN houses on students.house_id=houses.id
WHERE students_subjects.class_years_section_id=1
AND students_subjects.school_session_id=1 AND students.is_active=1
and it gives me house_name = NULL
Can anybody tell me how to get house name too . using join query
Thanks
The error in your query is caused by the LEFT JOIN keyword that is after WHERE clause,
SELECT students_subjects.student_id,
students.house_id,
students_subjects.subject_id,
subjects.subject_name,
students.rollno,
students.first_name,
students.last_name
FROM students_subjects
LEFT JOIN students
on students_subjects.student_id=students.id
LEFT JOIN subjects
on students_subjects.subject_id=subjects.id
LEFT JOIN houses
on students.house_id=houses.id
WHERE students_subjects.class_years_section_id = 1 AND
students_subjects.school_session_id = 1 AND
students.is_active = 1
Remember that JOINs are part of the FROM Clause.
UPDATE 1
SELECT b.student_id,
a.house_id,
b.subject_id,
c.subject_name,
a.rollno,
a.first_name,
a.last_name,
d.house_name
FROM students a
INNER JOIN students_subjects b
ON b.student_id = a.id
INNER JOIN subjects c
ON b.subject_id = c.id
INNER JOIN houses d
ON a.house_id = d.id
WHERE b.class_years_section_id = 1 AND
b.school_session_id = 1 AND
a.is_active = 1
You've miss placed the WHERE clause, try this:
SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name
FROM students_subjects
LEFT JOIN students ON students_subjects.student_id=students.id
LEFT JOIN subjects ON students_subjects.subject_id=subjects.id
LEFT JOIN houses ON students.house_id=houses.id
WHERE students_subjects.class_years_section_id=1
AND students_subjects.school_session_id=1 AND students.is_active=1