tables
sample data
walls
wall_id wall_name
1 wall_1
2 wall_2
6 wall_6
wall_categories
wall_id category_id
1 2
2 1
6 1
6 2
categories
category_id category_name
1 Wallpaper
2 Photography
html
Wallpaper
Photography
What I need to do is when the user click the Wallpaper link, I want the images with wallpaper as category to be displayed same with photography. I have built an initial query but it generates duplicate records as I'm using join, there must be something else that need to be added to make it work.
SELECT DISTINCT walls.wall_id, walls.wall_name, walls.wall_views, walls.upload_date,
categories.category_name FROM categories INNER JOIN wall_categories ON
wall_categories.category_id=categories.category_id INNER JOIN walls ON
walls.wall_id=wall_categories.wall_id;
or since the category_id is fixed, we can use walls and wall_categories table only. Then lets' say we can use the following html.
Wallpaper
Photography
You're not limiting your query by category_id, so it's returning all wall records that have an associated category.
SELECT DISTINCT walls.wall_id, walls.wall_name, walls.wall_views, walls.upload_date,
categories.category_name FROM categories INNER JOIN wall_categories ON
wall_categories.category_id=categories.category_id INNER JOIN walls ON
walls.wall_id=wall_categories.wall_id
WHERE category.category_id = ?
;
And then bind ? to the appropriate category ID from your user's selection.
Related
I have tried for a number of hours to get this. I am still quite new to mysql but have managed to achieve queries that I was impressed with after using the resources and examples I found. I am a bit stuck here. Apologies if I do not ask this very well.
Three tables that are used for managing categories and category membership within a project.
table a = project membership
id user_id project_id
== ======= ==========
1 1 10
2 1 12
3 3 45
4 5 12
table b = categories
id name project_id
== ==== ==========
1 cat1 10
2 cat4 12
3 cat8 45
tabke c = category members
id user_id_added category_id capability
== ============= =========== ==========
1 1 2 1
2 3 3 2
3 5 3 1
4 5 2 0
Required result
members of project 2
user_id category capability_in_category
======= ======== ======================
1 2 1
5 2 0
SELECT a.user_id
, c.capability
, b.id as category
FROM a
LEFT OUTER JOIN b
ON a.project_id = c.project_id
LEFT OUTER JOIN c
ON b.id = c.category_id
WHERE a.project_id = $project_id
AND c.category_id = $category_id;
It feels like I don't need to join the three tables, but I do not see a way of joining the project table with the category membership table without using the category table (b). The query I am running nearly works, but user capability is not returning correct. I am using left outer joins as a member may not always be part of a category, but they still need to be shown as a member in the project. I have been trying various joins and subqueries, without success. I basically need a list of the members in the project and if they are part of a category, to show the capability they have of the specific category. I feel there are a few ways of doing this potentially, but there is a gray area I am struggling to bridge.
The question is vague so I might help you to solve the wrong problem but if you want to have all members of a specific project listed (regardless of their capability) and to list the capabilities in a specified category listed as well, then:
SELECT project_memberships.user_id
, category_members.category_id AS category
, category_members.capability AS capability
FROM project_members
LEFT OUTER JOIN categories
ON project_members.project_id = categories.project_id
LEFT OUTER JOIN category_members
ON categories.id = category_members.category_id
AND category_members.user_id_added = project_membership.user_id
WHERE project_members.project_id = $project_id
AND (categories.id = $category_id OR categories.id IS NULL);
should get you that.
I altered tree things compared to your original query:
I used the table names as they are more speaking than "a, b, c"
I added the additional constraint category_members.user_id_added = project_membership.user_id to the second join so as to not join category_members of a different user to a project_members record.
I loosened the WHERE condition so that members not having the desired capability are also displayed. category and capability will be NULL for those records.
As to your question regarding having to join the three tables the answer is yes, you need to do that.
Having the following Schema
(ER of DataBase)
I am trying to create a query that
will show the title(Movies.Title) along with each movie's genre(movie_Genres.movie_genre) for each movie that..
that have been seen in (a) a specific time period (lets say 2-3 days for ex. We can take those days from the Tickets.ticket_date) (b) by Male Customers(Customer.customer_sec) and (c) got rated more than 4 (rated_customerRation) by those customers.
I can get as close as the following query:
SELECT
`movie_title`, `movie_Genres`.`movie_genre`
FROM
`Movies`
INNER JOIN
`movie_Genres` ON `mg_movie` = `movie_ID`
INNER JOIN
`Rated` ON `rated_movie_ID` = `movie_ID`
WHERE `rated_customerRatio` > 4 AND
UNION
(SELECT
`customer_sex`, `rated_customerRatio`
FROM
`Customer`
/* INNER JOIN
`cinema`.`Rated` ON `Rated`.`rated_customer_tabID` = `Customer`.`customer_tabID`
*/
INNER JOIN
`cinema`.`Tickets` ON `Tickets`.`ticket_customer_tabID`=`Customer`.`customer_tabID`
WHERE
`customer_sex` LIKE 'Male'
/* AND rated_customerRatio > 4 */
AND `Tickets`.`ticket_date` > '2016-02-16')
GROUP BY `movie_title`;
Also I am thinking that I will have trouble, cause one movie can have more than one Genre, and I don't want double lines, for the same movie, in my outcome.
Any help will be taken into serious regards!
use a WHERE movie.ID IN (Select movie.id...) query to have unusual query relationships. i.e. if movie.id can be related to tickets
WHERE MovieID IN
(
SELECT
MovieID
FROM
MovieTable
LEFT JOIN TICKETS ON MovieID = TICKETS.movieID
LEFT JOIN CUSTOMER ON TICKETS.ID = CUSTOMER.ID
WHERE
CUSTOMER.customer_sex LIKE 'Male'
AND Tickets.ticket_date > '2016-02-16')
)
I dont know your DB schema but that should give you a rough idea on one way of sorting based on viewership of moves from tickets? 1 ticket should have 1 movie ID and 1 customer, so the relationship is pretty easy to figure out
I have 2 tables. One is movie table, where all movie data is stored and another one is relation table, where I can see which movie is attached to which category. If I want to show movies from category 6 I join them like this:
SELECT t1.title, t2.category FROM movies t1
JOIN movie_categories t2 ON t1.movieid = t2.movieid
WHERE t2.category = 6
And it works just fine. However, movie SEF url are generated based on the first row from table2 (actually it should connect to table3, a category table, where i will find sef alias name, but for now I dont need that, I just want to know how to get first category). So lets say, movie have 3 categories, which will be looking like this:
Table 2 [relations table]
id movieid category
1 1 3
2 1 6
3 1 2
Table 1 [movie table]
movieid
1
2
The query I showed before returns results like this:
title category
bla1 6
bla2 6
However to create sef url, I need to get the first category id from relations table which is 3 in this case.
I'd like to know how to construct query to achieve the result I described.
ASSUMPTION: by MIN you mean the lowest ID in movie_Categories and then display it's category value.
Generate a set of data which is the lowest ID for each movie (subquery)
Join it back to the movie list
Then join your movie categories back to the generated set with the min value
Join the movie categories again back to the base movie set to get the limiting set for category.
Then limit the data base the category desired.
.
drop table movie;
drop table movie_categories;
create table movie (movieid int, Title varchar(10));
create table movie_categories (Id int, movieID int, category int);
insert into movie values (1, 'The');
insert into movie values (2, 'End');
insert into movie_categories values (1,1,3);
insert into movie_categories values (2,1,6);
insert into movie_categories values (3,1,2);
--To aid in understanding you may want to select * and see why this works and remove the where clause.
--Basically the two joins to Movie_Categories is once for the LOWEST ID and once again to get the limiting category.
SELECT M.Title, MC1.Category
FROM Movie M
INNER JOIN (SELECT min(ID) ID, MovieID
from movie_categories mc
GROUP BY MovieID) L
on L.MovieID = M.MovieID
INNER JOIN Movie_Categories MC1
on L.MovieID = MC1.MovieID
INNER JOIN Movie_Categories MC2
on l.movieid = mc2.movieid
and L.ID = MC1.ID
where mc2.category = 6
Results in:
title category
The 3
Note: the 2nd title isn't listed because there are no records in movie_category which match a category of 6.
I'm looking to join a 2 tables but the second table has a one to many relation. Can I omit the entire row if any of the lines have a certain value? Let me explain more.
User table
id name email
1 bob bob#test.com
2 foo foo#test.com
Music table
id userId
1 1
1 2
2 1
3 1
2 2
Say I don't want it to show the user if he has a relation to music table id 2. Also looking for distinct user.
If I try something like this it will still show both users.
SELECT * FROM users u LEFT JOIN music m ON u.id = m.userId WHERE m.id <> 3
I want it to check all the rows and if it has the id 3, it won't show. I hope I made sense. Thanks a lot.
Try using sub query like this:
SELECT * FROM users
WHERE id NOT IN (SELECT userId FROM music WHERE id=3)
This query means to select all users if their id is not related with music.id 3.
I have a MYSQL table called 'categories' from a project I inherited from someone else.
id parent_id name
1 NULL Travel
2 NULL Sleep
3 NULL Eat
4 NULL Bath
5 1 Prams
6 1 Travel Systems
7 2 Cots
8 3 High Chairs
The table is obviously a lot bigger than that, but you get the general idea. I have a MYSQL statement which brings together this table with other category, brand and product tables, but basically I want to list the parent category name from the above table with the sub-category in the statement. How do I do this?
My current statement is something like:
SELECT brands.name, products.name, categories.id, categories.name, brands.id,
FROM `products` , `brands` , `categories`
WHERE products.brand_id = brands.id
AND products.category_id = categories.id
AND brands.name = '$brand'
ORDER BY categories.name, products.name
How do I retrieve the parent category names in the results?
For example if the product is a Pram, how can I output "Travel". I could do seperate MYSQL statements in the loop but I want to avoid this. This is either a stupidly simple question (in which case I apologise for being brain dead) or a little more complicated! Thanks.
First you need to know the parent id of the current category and then get the name for that id, you could use a subquery in this way:
SELECT name FROM categories WHERE id = (SELECT pid FROM categories WHERE name = $brand)
EDIT: Since you need to get the category and subcategory names in the same row for a given subcategory id, try this:
SELECT sc.name AS subcategory, c.name AS category
FROM categories sc
LEFT JOIN categories c ON c.id = sc.parent
WHERE sc.id = $subcategory_id