I'm trying to knock the rust off my SQL skills and need some help with the following query. The database i'm currently using is mysql.
I want to retrieve all the FlashCards that are assigned BOTH 'tag2' and 'tag4'. Based on the contents of the existing table (as seen in the excerpt below) the query should return two rows: FlashCard_ID 1 and 2.
How would I formulate this query? It's been a while since I've had to do something like this.
mysql> select * from flashcard;
+--------------+------------+----------+
| FLASHCARD_ID | QUESTION | ANSWER |
+--------------+------------+----------+
| 1 | Question 1 | Answer 1 |
| 2 | Question 2 | Answer 2 |
| 3 | Question 3 | Answer 3 |
+--------------+------------+----------+
3 rows in set (0.00 sec)
mysql> select * from tag;
+--------+------+
| TAG_ID | NAME |
+--------+------+
| 1 | tag1 |
| 2 | tag2 |
| 3 | tag3 |
| 4 | tag4 |
| 5 | tag5 |
+--------+------+
5 rows in set (0.00 sec)
mysql> select * from flashcard_tags;
+--------+--------------+
| TAG_ID | FLASHCARD_ID |
+--------+--------------+
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 2 | 2 |
| 4 | 2 |
| 5 | 2 |
+--------+--------------+
6 rows in set (0.00 sec)
SELECT f.*
FROM (
SELECT flashcard_id
FROM tags t
JOIN flashcard_tags ft
ON ft.tag_id = t.tag_id
WHERE t.name IN ('tag2', 'tag4')
GROUP BY
flashcard_id
HAVING COUNT(*) = 2
) ft
JOIN flashcard f
ON f.flashcard_id = ft.flashcard_id
SELECT f.*
FROM flashcard f
INNER JOIN flashcard_tags ft1 ON f.FLASHCARD_ID = ft1.FLASHCARD_ID
INNER JOIN tag t1 ON ft1.TAG_ID = t1.TAG_ID AND t1.NAME = 'tag2'
INNER JOIN flashcard_tags ft2 ON f.FLASHCARD_ID = ft2.FLASHCARD_ID
INNER JOIN tag t2 ON ft2.TAG_ID = t2.TAG_ID AND t2.NAME = 'tag4'
Here's another query that works. This one doesn't use a subquery and is what I ended up using in my Hibernate code.
select fc.FLASHCARD_ID,
fc.QUESTION,
fc.ANSWER
from FLASHCARD fc
inner join FLASHCARD_TAGS fc_t
on fc.FLASHCARD_ID=fc_t.FLASHCARD_ID
inner join TAG t
on fc_t.TAG_ID=t.TAG_ID
where t.Name in ('tag2', 'tag4')
group by fc.FLASHCARD_ID
having count(t.TAG_ID)=2
Related
I am trying to join two tables and only select the dealers that did not have their promo code used on any order.
How can I do this?
I'm trying this below, but it's not working right. In the example I want to get just Bob, since his promo_code hasn't been used in any orders.
SELECT d.`name`
FROM z_dealer d
LEFT OUTER JOIN z_order o ON (d.promo_code = o.promo_code)
AND o.promo_code IS NULL
Here are my tables...
mysql> select * from z_dealer;
+----+------+------------+
| id | name | promo_code |
+----+------+------------+
| 1 | John | holiday |
| 2 | Suzy | special |
| 3 | Bob | laborday |
+----+------+------------+
mysql> Select * from z_order;
+----+-------+------------+
| id | total | promo_code |
+----+-------+------------+
| 1 | 10 | holiday |
| 2 | 20 | special |
| 3 | 15 | holiday |
| 4 | 45 | special |
+----+-------+------------+
SELECT d.`name` FROM z_dealer d LEFT JOIN z_order o ON (d.promo_code = o.promo_code) WHERE o.promo_code IS NULL
Have you tried INNER JOIN? or You can try IN like this :
SELECT d.name
FROM z_dealer d
WHERE d.promo_code not in( SELECT promo_code FROM z_order)
I'm not entirely sure why it's not working in your example code. I've created the same tables locally and when I run the script you provided I get the single 'Bob' answer.
SELECT d.name
FROM z_dealer d
LEFT OUTER JOIN z_order o ON (d.promo_code = o.promo_code)
AND o.promo_code IS NULL
What results are you seeing exactly?
Please forgive me in case a similar question has already been asked, but I was not able to find it. The problem with searching is that expressing the question is difficult in a few words, though the setup is quite simple:
Basically, I have entries that can be assigned to lists. For that purpose I have three tables:
mysql> SELECT * FROM list;
+-----+-----------+
| lid | listname |
+-----+-----------+
| 1 | Fine List |
| 2 | Bad List |
+-----+-----------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM entry;
+-----+-----------+
| eid | entryname |
+-----+-----------+
| 1 | red |
| 2 | green |
| 3 | blue |
| 4 | gray |
| 5 | black |
+-----+-----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM entry2list;
+-----+-----+
| eid | lid |
+-----+-----+
| 1 | 1 |
| 2 | 1 |
| 5 | 1 |
| 2 | 2 |
| 4 | 2 |
+-----+-----+
5 rows in set (0.00 sec)
So as a result I have 2 lists, one with 2 colors assigned, one with 3.
I can easily get a list with entries on a certain list:
SELECT * FROM list
LEFT JOIN entry2list USING (lid)
LEFT JOIN entry USING (eid)
WHERE lid=1
But I need the missing entries also, which is much more dificult:
SELECT tmp.eid, e.* FROM entry e
LEFT JOIN (
SELECT * FROM list
LEFT JOIN entry2list USING (lid)
LEFT JOIN entry USING (eid)
WHERE lid=2) AS tmp
USING (eid)
+---------+-----+-----------+
| tmp.eid | eid | entryname |
+---------+-----+-----------+
| NULL | 1 | red |
| 2 | 2 | green |
| NULL | 3 | blue |
| 2 | 4 | gray |
| NULL | 5 | black |
+---------+-----+-----------+
In my case, I need a yes/no list to see if an entry is in the list or not:
SELECT GROUP_CONCAT(IF(tmp.eid IS NULL, 'n', 'y')) AS is_set FROM entry e
LEFT JOIN (
SELECT * FROM list
LEFT JOIN entry2list USING (lid)
LEFT JOIN entry USING (eid)
WHERE lid=2) AS tmp
USING (eid)
which results in
+-----------+
| is_set |
+-----------+
| n,y,n,y,n |
+-----------+
But now I am stuck; The result is basically what I need, but the last demand on the request is that I need a similar row for every list, so the lid in the where class has be substituted somehow (or, more probable, the request has to be converted completely).
What I would like to get as a result is something like
+-----+-----------+
| lid | is_set |
+-----+-----------+
| 1 | y,y,y,n,n |
| 2 | n,y,n,y,n |
+-----+-----------+
Is that possible? How can it be done?
First, generate the rows that you need -- which are all lists and all entries. Then bring in the information about what belongs to what, and aggregate to get the strange form of output that you want.
The following produces what you want, with a separate row for each list and entry:
select l.listname, e.entryname,
(case when el.lid is null then 'n' else 'y' end) as InList
from list l cross join
entry e left join
entry2list el
on l.lid = el.lid and e.eid = el.eid;
For the format that you want, you need a group by and group concat. Something like this:
select l.lid, l.listname,
group_concat(case when el.lid is null then 'n' else 'y' end order by e.entryname) as ListFlags
from list l cross join
entry e left join
entry2list el
on l.lid = el.lid and e.eid = el.eid
group by l.lid;
I have two tables in MySQL:
Products:
id | value
================
1 | foo
2 | bar
3 | foobar
4 | barbar
And properties:
product_id | property_id
=============================
1 | 10
1 | 11
2 | 15
2 | 16
3 | 10
3 | 11
4 | 10
4 | 16
I want to get products that have determined properties.
For example I need to get all products that have properties with ids 10 and 11. And I expect products with ids 1 and 3 but not 4!
Is it possible in mysql or I need to use PHP for it?
Thank you!
with ids 10 and 11
Here's 2 solutions:
SELECT p.id,
p.value,
Count(DISTINCT propety_id)
FROM products p
INNER JOIN properties pr
ON p.id = pr.product_id
AND propety_id IN ( 10, 11 )
HAVING Count(DISTINCT propety_id) = 2;
or....
SELECT p.id,
p.value
FROM products p
INNER JOIN properties pr1
ON p.id = pr2.product_id
AND pr1.propety_id = 10
INNER JOIN properties pr2
ON p.id = pr2.product_id
AND pr2.propety_id = 11;
As for excluding rows - add a NOT exists clause, or do an additional left join and exclude matching rows.
SELECT *
FROM [products]
WHERE id IN (SELECT product_id
FROM [properties]
WHERE propety_id IN ( '10', '11' )
HAVING Count(DISTINCT propety_id) = 2);
Try this:
SELECT p.id
FROM product p
INNER JOIN properties prop
ON p.id = prop.product_id
AND property_id IN ( 10, 11 )
GROUP BY p.id
HAVING Count(DISTINCT property_id) = 2
Here is how I solved it:
mysql> SELECT * FROM products;
+----+--------+
| id | value |
+----+--------+
| 1 | foo |
| 2 | bar |
| 3 | foobar |
| 4 | barbar |
+----+--------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM properties;
+------------+-------------+
| product_id | property_id |
+------------+-------------+
| 1 | 10 |
| 1 | 11 |
| 2 | 15 |
| 2 | 16 |
| 3 | 10 |
| 3 | 11 |
| 4 | 10 |
| 4 | 16 |
+------------+-------------+
8 rows in set (0.00 sec)
Now we select all the product ids, that have property_ids IN (10, 11) and having 2 distinct rows for property_id:
mysql> SELECT
product_id FROM properties
WHERE
properties.property_id IN (10, 11)
GROUP BY
product_id
HAVING
COUNT(DISTINCT property_id) = 2;
+------------+
| product_id |
+------------+
| 1 |
| 3 |
+------------+
2 rows in set (0.01 sec)
Combining this query with SELECT-ing from products:
mysql> SELECT
id, value
FROM
products
WHERE
products.id IN(
SELECT
product_id FROM properties
WHERE
properties.property_id IN (10, 11)
GROUP BY
product_id
HAVING
COUNT(DISTINCT property_id) = 2);
+----+--------+
| id | value |
+----+--------+
| 1 | foo |
| 3 | foobar |
+----+--------+
2 rows in set (0.00 sec)
sql fiddle
Try this one
SELECT Prd.*
FROM products As Prd
LEFT JOIN (SELECT product_id ,SUM(RStatus) As Tt
FROM (SELECT product_id,
CASE
WHEN propety_id = 10 THEN NULL
WHEN propety_id = 11 THEN NULL
ELSE 0
END As RStatus
FROM properties
) A
GROUP BY product_id
) AS Prt ON(Prd.ID = Prt.product_id )
WHERE Prt.Tt IS NULL
I have two tables:
Products:
+-------------------------------------------------+
| id | name | category | price |
+-------------------------------------- ----------+
| 1 | item1 | 1 | 0.99 |
| 2 | item2 | 2 | 1.99 |
| 3 | item3 | 3 | 2.95 |
| 4 | item4 | 4 | 2.50 |
+-------------------------------------------------+
Images:
+--------------------------------------------------+
| id | file_name | p_id | priority |
+-------------------------------------- -----------+
| 1 | image1 | 1 | 0 |
| 2 | image2 | 1 | 1 |
| 3 | image3 | 2 | 2 |
| 4 | image4 | 3 | 2 |
| 5 | image5 | 3 | 3 |
| 11 | image6 | 3 | 5 |
| 16 | image7 | 4 | 1 |
| 19 | image8 | 4 | 7 |
+--------------------------------------------------+
I need to get all of product information, as well as the file name of an image for the product. Notice that a product can have more than one image; I want the one with the lowest priority. Also, I only want results for products that are in a certain category.
So, say I need information for products in categories {1,2,3}, then after the query runs the result should return:
+-----------------------------------------------------------------+
| id | name | category | price | file_name |
+-------------------------------------- ----------+---------------+
| 1 | item1 | 1 | 0.99 | image1 |
| 2 | item2 | 2 | 1.99 | image3 |
| 3 | item3 | 3 | 2.95 | image4 |
+-------------------------------------------------+---------------+
I have tried writing a couple of different join statements, but none of them work; not surprising, since I'm a total novice when it comes to SQL.
Any help would be greatly appreciated!
I will add a step by step tutorial, first getting the join right,
then adding some conditions to filter the category and finally, grouping
and using the having clause with a sub-select. You will need to use the last select
in your code. I also tested this on a mysql instance and it works.
I'm using group by in case you need some other complex stuff. It's good to have an example.
The syntax is ansii sql, it should work on all databases not just mysql
-- get everything by joining
select p.*, i.file_name
from products p
join image i on (p.id = i.p_id)
/* get everything by joining
* + filter by category
*/
select p.*, i.file_name
from products p
join image i on (p.id = i.p_id)
where p.category in (1,2,3)
/* get everything by joining
* + filter by category
* + image is the one with the lowest priority
* note: selecting the priority is not necessary
* but it's good for demonstration purposes
*/
select p.*, i.file_name, i.priority
from products p
join image i on (p.id = i.p_id)
where p.category in (1,2,3)
group by p.id
having i.priority = (select min(priority) from image where p_id = p.id)
This is the answer:
select a.id, a.name, a.category, a.price, b.filename as file_name
from products a left join (
select i.p_id, i.filename from (select id, min(priority) as min_p
from images group by p_id) q
left join images i on q.id = i.id
) b on a.id = b.p_id
where a.category in (1, 2, 3);
EXPLANATION:
First, you need to get a set where for each products with lowest priority, which is from this query:
select id, min(priority) as min_p from images group by p_id;
The result will be:
+----+----------+
| id | lowest_p |
+----+----------+
| 1 | 0 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
+----+----------+
4 rows in set (0.00 sec)
The next step will be to get an outer join, in this case I'd choose (arbitrarily according to my preference), the left join:
select i.p_id, i.filename from (select id, min(priority) as min_p
from images group by p_id) q left join images i on q.id = i.id;
This query produce what you want in short:
+------+----------+
| p_id | filename |
+------+----------+
| 1 | image1 |
| 2 | image3 |
| 3 | image4 |
| 4 | image7 |
+------+----------+
4 rows in set (0.00 sec)
Now you just need to decorate this, again using left join:
select a.id, a.name, a.category, a.price, b.filename as file_name
from products a left join (
select i.p_id, i.filename from (select id, min(priority) as min_p
from images group by p_id) q
left join images i on q.id = i.id
) b on a.id = b.p_id
where a.category in (1, 2, 3);
And you'll get what you want:
+------+-------+----------+-------+-----------+
| id | name | category | price | file_name |
+------+-------+----------+-------+-----------+
| 1 | item1 | 1 | 0.99 | image1 |
| 2 | item2 | 2 | 1.99 | image3 |
| 3 | item3 | 3 | 2.95 | image4 |
+------+-------+----------+-------+-----------+
3 rows in set (0.00 sec)
You can also put the products in the right hand side of the left join, depending on what you expected when there is product without images available. The query above will display the view as above, with the file_name field as "null".
On the other hand, it will not display any if you put products on the right hand side of hte left join.
Building on sarwar026's answer...
SELECT p.id, name, priority, price, file_name
FROM Products p, Images i
WHERE p.id = i.p_id
AND i.priority = (SELECT MIN(priority) FROM Images ii WHERE ii.p_id = p.id)
AND p.category IN (1,2,3)
(tested on a mysql database with copies of your tables)
My tables:
mysql> select * from pvf_order;
+------+------------+------+
| oid | orderdate | cid |
+------+------------+------+
| 1001 | 2014-02-10 | 1 |
10 rows in set (0.00 sec)
mysql> select * from pvf_order_item;
+------+-----+----------+
| oid | pid | quantity |
+------+-----+----------+
| 1001 | 1 | 2 |
18 rows in set (0.00 sec)
mysql> select * from pvf_product;
+-----+----------------------+----------------+--------+--------------+
| pid | prod_desc | Prod_finish | price | prod_line_id |
+-----+----------------------+----------------+--------+--------------+
| 1 | End Table | Cherry | 175.00 | 1 |
8 rows in set (0.01 sec)
Right now my query has been variations of:
SELECT
pvf_order.cid,
pvf_order.orderdate,
pvf_product.prod_desc,
pvf_order_item.quantity,
pvf_product.price
FROM pvf_order
INNER JOIN pvf_order.oid=pvf_order_item.oid
INNER JOIN pvf_order_item.pid=pvf_product.pid
WHERE YEAR(pvf_order.orderdate)=2014 AND MONTH(pvf_order.orderdate)=10;
I just don't know exactly where I'm going wrong. I edited some of the info out just to avoid a big wall of text, but I hope the general idea is there. Any help is appreciated!
Check JOIN sintaxis, also use ALIAS for table names
SELECT
o.cid,
o.orderdate,
p.prod_desc,
oi.quantity,
p.price
FROM pvf_order o
INNER JOIN pvf_order_item oi
ON o.oid = oi.oid
INNER JOIN pvf_product p
ON pvf_o.pid = p.pid
WHERE YEAR(o.orderdate) = 2014 AND MONTH(o.orderdate) = 10;
Also instead of use Year and Month function, you can simplify to one single comparasion operation.
WHERE date_format(o.orderdate, '%Y-%m') = '2014-10'