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';
Related
I'm trying to filter through categories assigned to topics based on which categories are assigned to each topic. I have three tables, a topic table (id, name), a category table (id, name), and a table that bridges the two table in a one-to-many relationship (id, topic_id, category_id).
For instance, I have a list of rides and shows at popular theme parks (Space Mountain, Thunder Mountain, It's a Small World, The Haunted Mansion, Tower Of Terror, Test Track, etc.), a list of categories (Magic Kingdom, Epcot, Animal Kingdom, Thrill Rides, Shows, Family Rides, Boat Rides, etc.), and an intermediate table linking them ((1,1,3), (1,1,5), (1,2,5), etc.).
SQLFiddle Example
I've got the basic query constructed for input of a single category, but I cannot figure out how to filter using multiple categories. For example, If I query categories 'magic-kingdom' AND 'nighttime-shows', that would filter out categories such as 'epcot', and 'thrill-rides'.
My query is as follows:
SELECT DISTINCT scat.name FROM category AS cat
JOIN bridge ON cat.id = bridge.cat_id
JOIN topic ON bridge.top_id = topic.id
JOIN bridge AS sbridge ON topic.id = sbridge.top_id
JOIN category AS scat ON sbridge.cat_id = scat.id
WHERE cat.slug = 'hollywood-studios'
ORDER BY scat.name ASC
Thank's for the help in advance!
As far as I understood, you need categories that match topics that match ALL of the filter categories.
You simply have to fill in the list of matching categories, and hardcode their count. Now it's 2
SELECT DISTINCT
cat.name
FROM bridge b
JOIN category cat
ON b.cat_id = cat.id
WHERE b.top_id IN (
SELECT
b.top_id
FROM bridge b
JOIN category c
ON c.id = b.cat_id
WHERE c.slug IN ('magic-kingdom', 'nighttime-shows')
GROUP BY b.top_id
HAVING count(b.cat_id) = 2
)
Use OR in where clause to filter records of multiple categories like
SELECT DISTINCT scat.name FROM category AS cat
JOIN bridge ON cat.id = bridge.cat_id
JOIN topic ON bridge.top_id = topic.id
JOIN bridge AS sbridge ON topic.id = sbridge.top_id
JOIN category AS scat ON sbridge.cat_id = scat.id
WHERE cat.slug = 'hollywood-studios' OR cat.slug = 'fireworks-shows'
ORDER BY scat.name ASC
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'
I'm trying to join 2 tables with same categories but it's not quite working, its not returning results.
I have 2 tables tvshows and movies and both of them have columns categories now i want to go through all of them and see if categories match and return them but I don't know what is wrong with this. Please take a look, thanks!
$query = $connection->prepare("
SELECT
a.id,
a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.id,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.categories = b.categories
WHERE a.categories LIKE ? OR b.categories LIKE ? ORDER BY a.id DESC
");
You are using column named categories in WHERE part of your query, but there are two columns with this name in tables movies and series, so database won't know, which column to search in.
You have to specify probably WHERE a.categories LIKE ? OR b.categories LIKE ? if you want to find rows, where some category is set for movie or series.
If you want to search for movies and series pairs by categories, you should join them by that column. But if categories is something like "Comedy,Action" etc. it won't work. You should consider remodeling database and create table categories and then movies_categories with ID of movie and ID of category rows and series_categories with same structure. Than you will be able to do lot's of queries for selecting movies that has categories same as series etc.
I'm not sure I've understood very well, but give this a try:
$query = $connection->prepare("
SELECT a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.hash = b.hash
WHERE a.categories = b.categories ORDER BY id DESC
");
I have to build a complex query on SugarCRMfor a report. SugarCRM relationship it's stored on some tables, so I need to join multiple tables using the relationship tables. The tables have a model and model_id which identify a table and record primary id.
Below is an example of what I am trying to do.
BLOGS : Main table with field ID
BLOG_ENTRY : Child table of Blog with with field ID, BLOG_ID (FK of BLOGS table) and POST_CONTENT_ID (FK of next table BLOG_CONTENT)
BLOG_CONTENT with field ID, MODEL (which identify a child table) and MODEL_ID (which identify child table id)
BLOG_CONTENT_TABLE_ONE with field id
Suppose we can have more than one table BLOG_CONTENT_TABLE_TWO, ...
The query should be something like:
select blogs.id, blog_content.model, blog_content.model_id, blog_content_table_one.id
from blogs, blog_entry, blog_content, blog_content_table_one
where
# join blog and blog_entry
blog.id = blog_entry.blog_id
# join blog_entry and blog_content
AND blog_entry.content_id = blog_content.id
# join blog_content and blog_content_table_one
AND blog_content.model = 'blog_content_table_one'
AND blog_content.model_id = blog_content_table_one.id
This works, but if there is more table, I suppose I should use a MySQL case ?
First, use the ON clause for JOINs. Second, I don't think it's possible to get all possible combinations, but for three blog content tables, you could go for something like this.
SELECT
blogs.id,
blog_content.model,
blog_content.model_id,
blog_content_table_one.id
blog_content_table_two.id
blog_content_table_three.id
FROM blogs
JOIN blog_entry
ON blog.id = blog_entry.blog_id
JOIN blog_content
ON blog_entry.content_id = blog_content.id
LEFT JOIN $blogtable1 blog_content_table_one
ON blog_content.model_id = blog_content_table_one.id
AND blog_content.model = '$blogtable1'
LEFT JOIN $blogtable2 blog_content_table_two
ON blog_content.model_id = blog_content_table_two.id
AND blog_content.model = '$blogtable2'
LEFT JOIN $blogtable3 blog_content_table_three
ON blog_content.model_id = blog_content_table_three.id
AND blog_content.model = '$blogtable3'
WHERE blog_content.model IN ('$blogtable1', '$blogtable2', '$blogtable3')
I need help regarding JOIN tables to get category name & author name from other tables.
for articles site I have Three Tables
articles
categories
users
Articles Table structure :
id
title
slug
content
category
author
date
I save category ID & user ID in articles table with each article
While Displaying Articles for a category I use this:
(just some draft idea not full code)
localhost/testsite/category.php?cat=category-slug
$catslug = ($_GET["cat"]);
//GET CAT ID from category table from slug
$qry = "SELECT * from category WHERE slug='$catslug';
$res = mysql_query($qry) or die("Error in Query");
$ans = mysql_fetch_array($res);
$cid = $ans['id'];
$catname = $ans['title'];
<h2><?php echo $catname; ?></h2>
//after getting cat-id query to get article of that ID
$aqry = select * from article where category=$cid ORDER BY date";
$ares = mysql_query($aqry) or die("Error in Query");
$aans = mysql_fetch_array($ares))
$aid = $aans['author'];
//then another query to get author name from users
select username from users where id=$aid
I m learning PHP & not much familiar with JOIN statement & combining queries
need help/suggestions hw can I achieve same with JOIN tables instead of different queries to get catname and author name from tables.
Thanks
This should do what you want
SELECT distinct c.title, U.Username
from category C join article A on A.category=C.id
join users U on U.id=A.author
WHERE C.slug= :catslug
Assuming Category id is foreign jey in article table
Try this query
select username from users
where id in (
select author from category cat
inner join article art
on cat.category_id =art.category_id
where cat.slug= $cat
order by art.date desc )
$q = "SELECT article.*
FROM article
LEFT JOIN category
ON article.category = category.ID
LEFT JOIN users
ON article.author = users.ID
WHERE article.slug = :slug";
:slug is where you would insert (bind if you're using PDO) $_GET['slug']
You can do some different thing with a SQL request. You can do some join in your SQL request but you can also do another SELECT statement in your request Like
SELECT SOMETHINGS FROM A_TABLE WHERE (SELECT ANOTHER_THING FROM ANOTHER_TABLE WHERE VALUE =1
But this statement will only work if you have 1 result in your second request
If you want to join two table with some value, you'll have to do it like this
SELECT something FROM a_table AS alias1 JOIN another_table AS alias2
ON alias1.value1 = alias2.value1 and .....
You can compare all the value that you want from one table to another and you can also join more than two table and the ON key word is not requested by sql syntax you can just do
SELECT * FROM table_1 join table_2
and it will show you all thje data from two table but be sure that this is the data you want this kind of little query can have some big result and slow down your app
you can read more there http://dev.mysql.com/doc/refman/5.0/en/join.html
First off, you should write a stored proc... but here's how you would get articles by categoryID that includes both the category name and user name.
Given the following table structures:
article
---------
articleID <- primary key
title
slug
content
categoryID <- foreign key
userID <- foreign key
createdDT
category
--------
categoryID <- primary key
categoryName
user
--------
userID <- primary key
userName
Here's how to write the query using joins:
SELECT a.title, a.slug, a.content, a.createdDT, c.categoryName, u.userName
FROM dbo.article a
JOIN dbo.category c on a.categoryID = c.categoryID
JOIN dbo.user u on a.userID = u.userID
WHERE a.categoryID = #categoryID
Here's an article from Microsoft on JOINs - http://msdn.microsoft.com/en-us/library/ms191517.aspx