I am working on a home project. In my database I have two table called movies and category. in the movies table there are three rows named category,category_two,category_three. And in the category table there are only one row name category_name.
I wanted to join all three rows from the movies table with the row named category_table from category table.
but I do not know how to do that query.
Please help me.
This is how you should do it:
movies table
------------
id
name
categories table
----------------
id
name
movie_categories table
----------------------
movie_id
category_id
Like this you can have any amount of categories for a movie. Then to get a specific movie along with its categories you can do:
select m.*, c.name as category_name
from movies m
left join movie_categories mc on m.id = mc.movie_id
left join categories c on c.id = mc.category_id
where m.name = 'star wars'
Related
I have 3 tables in my database, Categories,Subcategories and Articles.
My Articles table contains columns caled CategoryID and SubcategoryID, which are foreign keys of the table Categories and Subcategories.
My question is, how can I get the name of the Category and Subcategory stored only as ID's in my Articles Table.
I think I need a kind of subquery or join.
Here is what I have:
SELECT ArticleTitle
,CategoryID
,SubcategoryID
FROM Articles
WHERE SubcategoryID =
(SELECT SubcategoryName
FROM Subcategories
WHERE SubcategoryName = 'info'
)
When I execute this code in mysql, there are no erros, but I receive 0 results. All tables are containing some data.
change this:
where SubcategoryID = (select SubcategoryName from Subcategories
to this
where SubcategoryID in (select SubcategoryID from Subcategories
the changes were
the equal sign is now the word in.
the subquery is selecting SubcategoryID instead of SubCategoryName
Using Joins :
SELECT a.ArticleTitle AS ArticleTitle,
c.CategoryName AS CategoryName,s.SubcategoryName AS SubcategoryName
FROM
Articles a JOIN Categories c on a.CategoryID=c.ID JOIN Subcategories s on a.SubcategoryID=s.ID
WHERE s.SubcategoryName = 'info';
I have two tables, 1st table holds the categories list and 2nd table holds the profile information along with 1st table category id as a foreign key. As follow
Table1
id CATEGORY
1 first
2 second
Table2
id CATEGORY name phone
1 2 John 9999999999
how to retrieve table 2 records along with category name (not id:2 as shown in table 2)
I tried this,
SELECT category, name, phone FROM table2;
I need to see following line as result
second, john, 9999999999
get me out of this step, thanks in advance.
What you need is a JOIN, which means you "combine" two tables by linking rows from one table to rows from another table based on some criterion.
In your case, the criterion is that the value of CATEGORY in table2 must equal the value of ID in table1, which is expressed as follows:
SELECT table1.category,
table2.name,
table2.phone
FROM table2
JOIN table1
ON table2.category = table1.id
If needed, you can add a WHERE clause to limit the result to specific rows, e.g. WHERE table1.id = 9999999999 would filter the rows that have category 9999999999.
This should work:
SELECT t1.category, t2.name, t2.phone
FROM table2 AS t2
JOIN table1 AS t1
ON t1.id = t2.category
You can make an INNER JOIN and get the category from the Table 1, like this:
SELECT tb1.category, tb2.name, tb2.phone
FROM table2 tb2
INNER JOIN table1 tb1 on tb2.category = tb1.id
WHERE tb2.id = 1;
Hope it helps!
I have a problem with MySQL statement:
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends` ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers` ON oxarticles.OXID = oxmanufacturers.OXID
INNER JOIN `oxseo` ON oxarticles.OXID = oxseo.OXOBJECTID;
My problem is that tables oxarticles and oxmanufacturers have two same column names OXID and OXTITLE_1 but the above code doesn't work. Please help.
You are trying to match article id to manufacturer id. That's wrong. You need to join your article table to the manufacturer table using the manufacturer id, not the article id.
In your case, that is oxarticles.OXMANUFACTURERID for the manufacturer id in the article table and oxmanufacturers.OXID in the manufacturer table.
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends`
ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers`
ON oxarticles.OXMANUFACTURERID = oxmanufacturers.OXID
-- ^^^
-- Here's the manufacturer id
-- in the article table
INNER JOIN `oxseo`
ON oxarticles.OXID = oxseo.OXOBJECTID;
I have 2 tables:
Table A is a category table. Columns are cid, catname.
Table B is a relationship table. Columns are cid, parent (parent is another cid).
Here's where I am so far:
"SELECT c.cid, c.catname AS catname, r.parent AS parent FROM tableA AS c JOIN tableB AS r ON r.cid=c.cid";
I know I'll get 3 columns (2 from tableA and one from tableB) but I also want to get the catname value from the parent in tableA If I were to do a second query, it would look like this (assuming we put the result into a $row variable):
"SELECT catname FROM tableA WHERE cid='".$row['parent']."'";
That way I can display it as text.
What do I add, and where? Is there a second JOIN?
You can join a table multiple times:
SELECT a.cid AS acid, a.catname AS aname,
b.cid AS bcid, b.catname AS bname
FROM relationships AS r
JOIN categories AS a ON (r.cid = a.cid)
JOIN categories AS b ON (r.parent = b.cid)
Given the following tables I would like to know how to write a query to return only the categories that have books in them and the number of books for each category. A book can be added in one or many categories. I'm using PHP and MySQL.
Here are my tables:
categories table
- id
- name
- permalink
books table
- id
- title
- author
- description
- price
books_categories table
- id
- book_id
- category_id
select c.id
,c.name
,count(*) as num_books
from categories c
join books_categories bc on(bc.category_id = c.id)
group
by c.id
,c.name;
Use LEFT JOIN if you also want the categories without books.
SELECT *, COUNT(*) AS count
FROM books_categories AS bc
LEFT JOIN categories AS c ON c.id = bc.category_id
GROUP BY c.id
You'll get a count column with the number of rows for each category. Categories with zero books (that is those with no entry in table books_categories) won't be returned.
SELECT categories.id,
COUNT(books_categories.id) AS number
FROM categories
LEFT JOIN books_categories ON books_categories.category_id = categories.id
GROUP BY categories.id
HAVING number > 0;