MySQL view - many to one relationship in one row - mysql

I have a table that holds the movie information. It has ID, title and so on. I have another table called categories where I have the available categories; action, drama and so on.
Each movie can be in many categories. So I created a view and joined these tables. Now the view displays a row for each category even if the movie is repeated.
I need to have have a single row for each movie and have categories as something like : 'Action, Drama, Comedy' (which is basically all categories from tbl_movies_categories).
How should I join tables/create view to achieve this?

This is the proper way:
Group By a unique ID, then use group_concat() to concat the values together.
CREATE VIEW `vw_metadata` AS
select
`vw_movies`.`id` AS `id`,
...
group_concat(`vw_movies_categories`.`title_category`
separator ',') AS `categories`,
from
(`vw_movies`
left join `vw_movies_categories` ON (`vw_movies_categories`.`movie_id` = `vw_movies`.`id`))
group by `vw_movies`.`id`

Related

Get results of three queries into one table

Here is the situation,
I have an interface with a dropdown(All, Category, Category type, Product) and search button. There are three database tables for each section as CATEGORY, CATEGORYTYPE and PRODUCT
When user select the All option search, I am getting results from all three tables and display on a HTML table.
TO do pagination, I want to fetch results set by set from all the tables.
Here are the three queries I'm using:
SELECT CATNAME,CATDESC,CATSTATUS FROM CATEGORY WHERE CATNAME like %a%
SELECT CATTYPENAME,CATTYPEDESC,CATTYPESTATUS FROM CATEGORYTYPE WHERE CATTYPENAME like %a%
SELECT PRODUCTNAME,PRODUCTDESC,PRODUCTSTATUS FROM PRODUCT WHERE PRODUCTNAME like %a%
From all the tables I'm only fetching the same set of column I want to combine the three queries. I hope my question is understandable. This is not about joining the three tables.. I want the results from all three queries to act as a one set or records and paginate them.
Thanks in advance for any suggestions and please ask if the question is not clear.
The application is JAVA EE based. Struts used and DB is MySQL.
Using view u can do what you want.
in very first u have to change columns heading as same. like
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORY WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORYTYPE WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM PRODUCT WHERE NAME like %a%
then you can create view like this
CREATE VIEW V AS
SELECT *
FROM ((CATEGORY NATURAL FULL OUTER JOIN CATEGORYTYPE)
NATURAL FULL OUTER JOIN PRODUCT);
P.S. if you can not change columns heading,you can have to change view according to that tables' columns

create MySQL View with categories table

i have a bit of a problem, i have two MySQL tables namely products and categories. The categories table has an id as the primary key and name of the category. The category id is a foreign key in the products table. I want to create a view, but i don't want the view to contain the id as the category,instead i want the category name to be shown in the view. I have tried every kind of JOIN but I'm still not getting the required result. What am i doing wrong? here is my latest join. Which is pretty much the closest to what i want.
SELECT c.title as title,ct.name as category, c.picture as picture
FROM companies c left join categories ct on ct.id=c.category
I have successfully gotten the title and picture, but the category remains null.
Thanks for the replies.. Michael turns out that you were right , I mixed up the tables when performing the insert via my web interface so there wasn't a match. Thanks a lot!

Selecting any one of the IDs of duplicate values to get only unique columns from a table

I have a website with products where some products are duplicated, and the duplication is only because sometimes the same products goes under more than one categories. I just want the unique columns of the product, not the duplicate (that has another ID and another Category_id). I know the problem could be solved if the table was normalized, but I didn't develop these tables and I can't redesign the database now.
So basically I'm trying to something that logically looks like this (but the code below still gets the repeated products):
SELECT id
FROM `website_products`
WHERE p_name_en
IN (
SELECT DISTINCT p_name_en
FROM `website_products`
)
Do you just want:
select distinct id
from website_products
Or are you trying to get distinct product names with a single id:
select p_name_en, id
from website_products wp
group by p_name_en;
You Can Try like this,,,
SELECT id
FROM `website_products
group by p_name_en

Joins on MySQL many-to-many tables

This has been driving me mad.
I have three tables:
items
ID
name
type
cats
ID
name
items_to_cats
FK_ITEM_ID
FK_CAT_ID
This is a simple many-to-many relationship. I have items and categories. Each item can be linked to one or more categories. This is done via a simple joining table where each row maintains a relationship between one item and one category using foreign key constraints.
You will notice that my "items" table has a field called "type". This is an indexed column that defines the type of content stored there. Example values here are "report", "interview", "opinion", etc.
Here's the question. I want to retrieve a list of categories that have at least one item of type "report".
Ideally I want to get the result in a single query using joins. Help!
select distinct cats.id, cats.name
from cats
join items_to_cats on items_to_cats.fk_cat_id=cats.id
join items on items.id=items_to_cats.fk_item_id
where items.type='report'
Just as a point of database design, if you have a small set of legal values for items.type, i.e. "report", "interview", "opinion", maybe a couple more, then you really should create a separate table for that with, say, an id and a name, then just put the type id in the items table. That way you don't get into trouble because somewhere it's mis-spelled "raport", or even more likely, someone puts "reports" instead of "report".
or how about this :
SELECT c.id, c.name
FROM cats c
WHERE c.id IN
(SELECT ic.fk_cat_id
FROM items_to_cats ic
JOIN items i on i.id=ic.fk_item_id
WHERE items.type='report'
)

MySQL: grab one row from each category, but remove duplicate rows posted in multiple categories

I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.
I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.
My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).
I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).
Thanks!
Matt
SELECT T.categoryName, tR.headline, tR.reviewText
FROM (
SELECT tC.categoryName, MAX(tR1.reviewID) reviewID
FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID
join tblCategory tC on tC.categoryID = tWC.categoryID
GROUP BY tC.categoryName) T JOIN
tblReview.tR on tR.reviewID = T.reviewID
this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')
Try using SELECT DISTINCT. (This will only work if your SELECT is only pulling the article ID.)
select DISTINCT reviewID