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
");
Related
Hello I am trying to sort through these 3 Tables
I need to create a query that goes through the 'Author' Table,
grabs the author num
Then goes to the 'Wrote' table to find the 'BookCode' from the AuthorNum of the last table
Then to finally go through the Book table to list the title of the book and the first name and last name of the author.
I was thinking of using a join table but am not too solid on my uinderstanding on how it works. Nested select statements was my next guess but I can't get them to go through so many tables.
If anyone could help me that would be fantastic, thank you.
You want to use INNER JOINS to match up the data
SELECT *
FROM authors AS a
INNER JOIN wrote AS w
ON a.AuthorNum = w.AuthorNum
INNER JOIN book AS b
ON w.BookCode = b.BookCode
Please try to use this :
(I named the first table name to first)
Select a.Title as title, w.AuthorFirst as firstName, w.AuthorLast as lastName
From wrote as w
Inner Join author as a
Inner Join first as f
On (Select ww.AuthorNum From WroteTable as ww Order By DESC LIMIT 1) = f.AuthorNum
On f.BookCode = a.BookCode
I have a table structure like following:
Users => Histories <= Brands
Histories table keeps following columns: userId, brandId, points
I have a query like following:
select b.id, sum(h.points),h.brandId
from brands b left join
histories h
on b.id = h.brandid and h.userId = 2866
group by b.id;
This query returns me brands where USER made points and didnt make any points at all..
I wanna add a filter to a brandId as well so that the query doesn't returns all results, but a single result at a time depending what brandId has been sent to the query... How can I and where can I add a statement which would return me a single record for this query?? So basically I just need to pass brandId = to something statement, leaving the query it self as it is right now ... :)
For example if brand id is 100:
select b.id, sum(h.points),h.brandId
from brands b left join
histories h
on b.id = h.brandid and h.userId = 2866
where b.id=100
group by b.id
limit 1;
I have a two tables, one with cities (id and city name) and one with pictures of the cities (city_id, etc).
Let's say I'm looking for the city called Sibiu. That should return 3 results, since there are more cities like that in the table (Miercurea Sibiului, Sibiu, Poiana Sibiului), but it only returns one.
Also, as a note, the timeline_elements doesn't have any pictures of the city yet.
SELECT cities_countries.*, COUNT(timeline_elements.city_id) as number_of_photos
FROM cities_countries
LEFT JOIN timeline_elements on (cities_countries.id = timeline_elements.city_id)
WHERE cities_countries.name LIKE '%Sibiu%'
Add the GROUP BY and also explicitly mention all the column names for the cities_countries table. I consider these are the columns in the cities_countries table. id, city_id, city_name.
Also set alias name for each table for the better readability.
SELECT C.id, C.city_id, C.city_name, ....
, COUNT(T.city_id) as number_of_photos
FROM cities_countries C
LEFT JOIN timeline_elements T ON C.id = T.city_id
WHERE C.name LIKE '%Sibiu%'
GROUP BY C.id, C.city_id, C.city_name, ....
I've two tables artist and members. artist has zero to many relation with members. (one artist may have 0 or many members)
So, I want to get all the fields of artist with the total number of members, that he has.
I write a query, but, it returns 0 count if there is no artist in the table (artist table is empty). I am using this query
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
It should simply not return anything if there is no artist.
Thank you!
I'm assuming that your field name is artistname in the artist table,
The query should go like this:
SELECT artist.artistname, COUNT(members.id) AS total_members
FROM artist
LEFT JOIN members ON members.artis_id = artist.id
GROUP BY artist.artistname
Use join, and to use sql count you need to use group by.
Perhaps the artistname is empty. Did you check the data? You could try the following modified query:
SELECT a.artistname, count(m.id) as total
FROM artist a, members m
WHERE m.artist_id = a.id
AND a.artistname is NOT NULL
AND a.artistname <> '';
This should work:
SELECT a.artistname,
count(m.id)
FROM artist a
LEFT JOIN members m
ON m.artist_id = a.id;
I have a table vm_category in which I fetch all rows from the category_id and caregory_name columns. I then use this in a drop-down to populate a search-filter module on the left side of the side.
Now doing so gives me a gigantic list of categories, and I'd like to narrow it down by category's sub-category. Those are organized in another table called vm_category_xref in which I have 3 columns;
category_parent_id
category_child_id
category_list
What I'd like to do is specify in my query to only return the category_id and names that match a specific category_parent_id in the _xref table. Here is my current query:
$query = "SELECT `c`.`category_name` AS `name` , `c`.`category_id` AS `id`
FROM `#__vm_category` AS `c` ORDER BY `c`.`category_id`;";
I am not familiar with JOIN enough to figure this out, but I know it should be rather simple, and any help would be much appreciated. Thank you!
Try this:
SELECT
c.category_name AS name,
c.category_id AS id
FROM #__vm_category AS c
INNER JOIN vm_category_xref xr
ON xr.category_child_id = c.category_id
WHERE xr.category_parent_id = 5 /* the parent_id you want to filter on */
ORDER BY c.category_id;
Try this:
SELECT c.category_name as name, c.category_id as id
FROM vm_category as c
INNER JOIN vm_category_xref as x ON
x.category_parent_id = c.category_id
ORDER BY c.category_id
Essentially this returns category_name and category_id only for records that have a category_id that equals a category_parent_id in _xref.
SELECT c.category_name AS name , c.category_id AS id
FROM vm_category c
INNER JOIN vm_category_xref x on c.category_id = x.category_child_id
ORDER BY c.category_id
I may have picked the wrong fields to join on but whichever are the 2 same fields in c and x are the ones you should put in the join condition
select vc.category_id, vc.category_name
from vm_category vc, vm_category_xref vcx
where vc.category_id = vc.category_parent_id
SELECT
c.category_name AS name
,c.category_id AS id
FROM
vm_category AS c
join vm_category_xref vcx
on c.category_id = vcx.category_parent_id
where vcx.category_parent_id = ?
ORDER BY c.category_id;
Replace ? with the id you want to match.
If I'm understanding you correctly, you want to retrieve category_name and category_ID only for certain category parent IDs on the xref.
I typically use IN for situations like these.
I'm assuming that category_id in vm_category maps to category_parent_id in vm_category_xref.
So...
SELECT CATEGORY_NAME, CATEGORY_ID
FROM VM_CATEGORY
WHERE CATEGORY_ID IN
(SELECT CATEGORY_PARENT_ID FROM VM_CATEGORY_XREF
WHERE CATEGORY_PARENT_ID IN ('XXX', 'YYY', 'ZZZ'))
XXX, YYY, ZZZ are the IDs for the categories you want.