Select from pivot table using and condition [duplicate] - mysql

This question already has answers here:
Select values that meet different conditions on different rows?
(6 answers)
Closed 4 years ago.
I'm struggling for hours with something that seems so easy but I cant make it to work or find something similar in Google.
I have 2 tables
images
and
tags
They have a relation of Many to Many so I have another pivot table named image_tag
What Im trying to achive is select all images that has tag_id=4 and tag_id=1
My first attemp was something like this:
SELECT * from images as a INNER JOIN
image_tag as b on a.id=b.image_id
WHERE b.tag_id=4 and b.tag_id=1
Of couse this gave me 0 result as you cant use AND condition directly to pivot.
After that I tried this:
SELECT * FROM images as a
INNER JOIN image_tag as b on a.id=b.image_id
WHERE b.tag_id IN (1,4)
This returns all the images that either has tag_id=1 or tag_id=4 tried also Inner joining the same pivot table but cant make it to work no matter what
EDIT: Adding the sql fiddle. http://sqlfiddle.com/#!9/1726b0/1 the result should be images with ids 4,5,6,7

Use group by and having to get all image_id's meeting the criteria and use the resulting id's for join.
SELECT a.*
FROM images as a
INNER JOIN (select image_id
from image_tag
where tag_id IN (1,4)
group by image_id
having count(distinct tag_id)=2
) b on a.id=b.image_id

You can do this with just the table image_tag. You have to join it with itself in order to get all the combinations. This way you can then select the rows witch will have both tags.
SELECT a.image_id
FROM image_tag as a
inner join image_tag as b on a.image_id=b.image_id
where a.tag_id=4 and b.tag_id=1

Related

mysql checking if product ID also exist in other table [duplicate]

This question already has answers here:
Select rows which are not present in other table
(4 answers)
Closed 2 years ago.
I have not much experience with JOINS and the result I get with query below isn't correct.
I have a table called products and want to check if there are records in the table product_links.
I only want to get a list of items that doesn't have rows in product_links.
When I run the below query, I only get one line.
Anybody suggestions? Google couldn't help me or I'm searching with the wrong keywords.
SELECT a.id, a.SKU, a.title,
(SELECT COUNT(b.id) AS amount FROM product_links WHERE b.product=a.id) AS amount
FROM products AS a
LEFT JOIN product_links AS b ON b.product=a.id
I would recommend not exists:
select p.*
from products p
where not exists (select 1 from product_links pl where pl.product_id = p.id)
From your question i understand you need info of products which doesnt have any links.
Below is the query for that
SELECT * FROM products
WHERE id NOT IN (SELECT id FROM product_links);

How to replace many to many reference on join in MySQL? [duplicate]

This question already has answers here:
Joining three tables using MySQL
(11 answers)
How to join multiple tables in MySQL?
(4 answers)
Closed 3 years ago.
I create a simple data base on 3 tables: Author, that can has many Books, and Genre that also can has many Books.
I want to get all Genres of one Author using it authorId. I wrote next nested SQL select:
SELECT genre.id, genre.genre_id, genre.genre_name, genre.genre_descr FROM (SELECT book.genre_id FROM book WHERE book.author_id = 7654) AS b JOIN genre WHERE b.genre_id = genre.genre_id;
It works good for me, but is it possible some minimize this select? For example, get rid of nested select?
Yes, I know, that the good solution is to create reference many to many beetwen genre and author, but it will add some complications...
Thanks!
You are almost there. A simple join will do
SELECT DISTINCT g.* FROM genre g JOIN book b ON g.id = b.genre_id AND b.author_id = 7654
If you slow reads, then index on author_id should just work.
One possible solution without using join:
select * from genre where id in (select genre_id from book where author_id = 7654)

Table inner join or selecting mutiple tables [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
INNER JOIN ON vs WHERE clause
INNER JOIN:
SELECT *
FROM PETS
INNER JOIN OWNER ON PETS.OWNER_ID = 1
AND OWNER.ID = 1
MULTIPLE TABLE SELECT:
SELECT *
FROM PETS, OWNER
WHERE PETS.OWNER_ID = 1
AND OWNER.ID = 1
Is one better than the other?
Faster than the other?
They seem to produce exactly the same results.
Is there any difference at all?
I am trying to learn the best methods. In using the join, I noticed that the exact same thing can be achieved with the multiple table call
read this answer about the difference between a cross join and an inner join Performance of inner join compared to cross join

MySQL query multiple table inner join [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I have this MySQL query but it seems to be getting an error while I try to run it. Since I'm a newbie I'd like some advice of what I should do to correct it. I just want to show the name, quantity and order date of the orders that has 1 or more pending products. Thanks a lot!
select product.name, order_details.quantity, order.date from product,order_details,order
inner join order on order_details.order_id=order.id
inner join product on order_details.product_id=product.id
inner join customer on order.cust_id=costumer.id WHERE order.pending=>1
You have a table called order. This word has special significance in SQL. Your options are to rename the table, or quote it whenever you want to query from it.
Easiest solution is to change.
inner join order ....
to
inner join `order`
Be sure to use back-quotes around the table name.
You have a table named 'order', which is a reserved word in SQL.
One solution is to prefix the table name with the database name as explained in Craic Computing blog
Another one is to wrap the table name with the ` character as you can read in this StackOverflow question
You can try something like :
SELECT product.name, order_details.quantity, `order`.date
FROM product
INNER JOIN order_details ON product.id = order_detail.product_id
INNER JOIN `order` ON `order`.id = order_detail.order_id
WHERE `order`.pending >= 1
As said in other answers, orderis a reserved keyword in SQL, surround it with backquotes.
Maybe you should store the pending information in the order_detail table (1 if pending, 0 if not), in order to keep track of which product is still pending instead of incrementing/decrementing the order.pending field.
In this case, you could make the following query :
SELECT product.name, order_details.quantity, `order`.date
FROM product
INNER JOIN order_details ON product.id = order_detail.product_id
INNER JOIN `order` ON `order`.id = order_detail.order_id
WHERE `order_detail`.pending = 1
Which would return all the products still pending in your orders instead of every product from orders in which maybe only one is pending.

is there need of RIGHT join, because we can achieve the same result using LEFT join by just altering the table name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
right join versus left join
is there any need of RIGHT join, bacause we can achieve the same result using LEFT join by just altering the table name
i have two tables Persons and Orders
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
and
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
and i have a query for left join as:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName
and with right join:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Orders
RIGHT JOIN Persons
ON Orders.P_Id=Persons.P_Id
ORDER BY Persons.LastName
both gives the same result.
Both versions exist for convenience. Only one would be necessary, as you say.
However, sometimes a large query would have to be changed quite a bit if there were only one (RIGHT or LEFT) and you wanted to add a non-inner join to the query without changing it much.
With both as an option, it's usually easy to work a non-inner join into a pre-existing query without too much other reworking of the query.
At least that's the only time I find myself writing a RIGHT JOIN: when I already have a query and need to add in a non-inner join and making it a LEFT JOIN would cause me to rework the query more than I want to.
Check this for more understanding
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html