I need a way to join a table with the results of a SELECT query, and I'm facing some difficulty to do that ...
There are 3 tables:
food_shops
id, name, slug
categories_food_shops
id, id_category, id_food_shop
categories
id, name, slug
How can I do to show a result with: food_shop.id, food_shop.name, categorie.slug
based on the categories_food_shops table? that contains a registry of the shop and their category?
so far I was able to do this, but it gets
Unknown column 'fs.id' in 'where clause'
SELECT fs.id, fs.slug, fs.name
FROM food_shops fs
JOIN
(
SELECT *
FROM categories_food_shops cfs
WHERE cfs.id_food_shop = fs.id
) AS cfs ON categories.id = cfs.id_catedory
Any help would be very appreciated, I'm new to this join statements
Try this:
SELECT
food_shops.id, food_shops.name, categories.slug
FROM food_shops
INNER JOIN categories_food_shops
ON food_shops.id = categories_food_shops.id_food_shop
INNER JOIN categories
ON categories_food_shops.id_category = categories.id
You can then use a WHERE statement at the end to display food shops with a specific id, or with a similar name, etc.
Related
I am trying to develop an enquiry management system for an institute.
I have 3 tables in mysql db.
leads_course_category table keeps course categories and has the columns: category_id, category_name, category_created_time, category_deleted.
leads_course_details table stores courses under categories and has the columns: course_id, course_category_id, course_name.
leads_enquiry_details table stores enquiry details and has the columns: enquiry_id, enquiry_name, leads_course_details_course_id, enquiry_deleted
I need to find the number of enquiries for each category.
I tried the following query:
SELECT category_name,COUNT(*) as COUNT
FROM leads_course_category
RIGHT JOIN leads_course_details
on category_id=leads_course_details.course_category_id
RIGHT JOIN `leads_enquiry_details`
on leads_course_details.course_id= leads_enquiry_details.leads_course_details_course_id
WHERE leads_enquiry_details.enquiry_deleted=1
GROUP BY leads_course_category.category_id
This query skips all the categories having null values, but I need to show that as count of zero.
Please help me to resolve this.
The condition enquiry_deleted = 1 must be moved to the ON clause:
select c.category_name, count(ed.enquiry_id) count
from leads_course_category c
left join leads_course_details cd on c.category_id = cd.course_category_id
left join leads_enquiry_details ed on cd.course_id = ed.leads_course_details_course_id and ed.enquiry_deleted = 1
group by c.category_id, c.category_name
I have got the following mySQL tables:
movies:
id,
title,
year
directors:
id,
name
directors_in_movies:
director_id,
movie_id
I would like to search for all the movies in which a director has been involved.
But I don't really know if I can combine three tables. I am quite new with databanks and I would be glad about some help how to write the SQL statement.
The exists operator should do the trick:
SELECT *
FROM movies m
WHERE EXISTS (SELECT *
FROM directors_in_movies dim
JOIN directors d ON dim.director_id = d.id
WHERE dim.movie_id = m.id AND
d.name = 'Some Director')
I have four table like the picture bellow
I want to count how many student that have status 'v' where in table submission have submission type '1' and group by student_id so in the last i can get table like this
I have try sql query like this
select p.id, (SELECT count(*) FROM (select b.id from student as a , submission as b WHERE a.id = b.student_id and b.id_submission_type =1 and a.status_n='v' and a.id_academic_programe = p.id GROUP BY b.student_id) ) from academic_programe as p
But give me error
1054 - Unknown column 'p.id' in 'where clause'
Any suggestion? sory for my english
Correlations cannot be in nested subqueries. Fortunately, this is easy to fix:
select p.id,
(select count(*)
from student st join
submission su
on st.id = su.student_id and
su.id_submission_type = 1 and
st.status_n = 'v' and
where st.id_academic_programe = p.id
)
from academic_programe p;
Try this out:
select c.academic_program_name,count(a.distinct student_name) as count
from
(select * from student where status = 'v') a
inner join
(select * from submission id_submission_type=1) b
on a.id =b.student_id
inner join
academic_program_name c
on a.id_academic_programe = c.id
group by c.academic_program_name;
Let me know in case of any queries.
Please try the following...
SELECT student.id,
student_name,
academic_program_name AS Programe,
COUNT( status_n ) AS status_n_count
FROM student
JOIN Submission ON student.id = Submission.student_id
RIGHT JOIN academic_program ON student.id_academic_programe = academic_program.id
WHERE id_submission_type = 1
AND status_n = 'v'
GROUP BY student.id,
student_name,
academic_program_name;
This statement begins by joining the student and Submission so as to get a table containing the student's id, student_name, status_n and id_submission_type fields. This is then RIGHT JOINed to form a table where each academic program is listed along with each student's details, and that programs with no students are still listed.
The resulting dataset is refined as per your criteria with the WHERE clause, GROUPed and SELECTed
If you have any questions or comments then please feel free to post a Comment accordingly.
Forgive me if I get some terms wrong, still trying to learn advanced mySql queries. I am trying to export data from a ecommerce platform, and some of the data I need is in lookup tables, the issue is the one lookup table I have can have more than 1 value associated, and the system I need to get the data into requires a specific format for that column.
I am using zen cart as my source if that helps, and below is the query.
Select zp.products_id as id, zpd.products_name as name, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
LEFT JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What I need is theres a look up table that tells me what categories belong to each product, I know it's going to be another join, but I need it so if more than 1 category belongs to a product to put it in the same column, and join them with a # symbol, and its not the id's of the category, its the path, so I need to look up the category ID to another table to get the path.
So "category1/subcategory#category2" for example..
Thanks for any guidance.
EDIT: I still need to get the results merged as I am getting double the results I want, but I see a record for each category it's in with this..
Select zp.products_id as id, zpd.products_name as name, zcd.categories_name as categories, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What you are looking for is group_concat(). I think it would be something like this:
Select zp.products_id as id, zpd.products_name as name,
group_concat(zcd.categories_name separator '#') as categories,
products_price_w as cost, products_price as price,
zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
group by zp.products_id
ORDER BY zp.products_id ASC;
Sorry for my english, I'm from austria, but I couldn't find another nice q&a-board. :)
I have a very strange structured mysql-database.
First there is a table for users, for my example there are only three colums interesting:
id, name, email
My second table is a table for events: id, name
My third table is a category-table for events: id, name, eventID
My fourth table is a undercategory-table for events: id, name, catID
My fifth table is a time-table for events: id, name, ucatID
My sixth table is a table, where a entry combines a time with a person
Its a plan for work, in some way...
users: id, name, email
events: id, name
categories: id, name, eventID
undercategories: id, name, catID
underundercategories: id, name, ucatID
table to join: id, userID, uucatID
Yeah I have, so far, every entry (user-name, ucat-name, uucat-name) ...
but I'm trying to get a list of persons who have no entry in the join-table! (but only, where the eventID is ... say 1^^)
Here is my code for the list where there is an entry:
SELECT a.*,b.name,c.name AS zeit,d.name AS kategorie
FROM intern_user AS b
INNER JOIN intern_dienstplan AS a ON a.userID=b.id
INNER JOIN intern_events_uucat AS c ON a.uucatID=c.id
INNER JOIN intern_events_ucat AS d ON c.ucatID=d.id
INNER JOIN intern_events_cat AS e ON d.catID=e.id
WHERE e.eventID='".$_POST['eventid']."'
ORDER BY b.name ASC
I hope someone can help me... I have already tried something with "a.id is null" (where is no entry in join-table), but it doesn't work.
A long time has gone... and I have found the solution for my problem. :]
Here the code, for others with the same problem...
SELECT *
FROM intern_user
WHERE
(
id NOT IN
(
SELECT a.userID
FROM intern_dienstplan AS a
INNER JOIN intern_events_uucat AS b ON a.uucatID=b.id
INNER JOIN intern_events_ucat as c ON b.ucatID=c.id
INNER JOIN intern_events_cat AS d ON c.catID=d.id
WHERE d.eventID='".$_POST['eventid']."'
GROUP BY a.userID
HAVING sum(b.wert) >= 100
)
)
AND status='1'
Thanks for all help and have fun with this shit. :D
Greets
It's not clear what is type of connection for categories-subcategories-subsubcategories.
How much subcategories could have the event (0-1-N)?
If you just want to find persons who do not have matched record in link table, you can use a query:
select a.*
from user a
left join link b on a.id = b.userId
where b.userId is null
or
select a.*
from user a
where id not in (select userId from link)
Update. Still do not have answer for the question above.
You can try:
select *
from users
where id not in (
select link.userId
from event a
inner join categories ...
inner join subcategories ...
inner join categories ...
inner join link ...
where a.eventId = ...)
It could be easy transformed to versions with left join or exists().