I have 3 tables product, version and Currency.
Is it possible to query products that have the MAX version but has not yet have any currency where currency version_id = the id of the MAX version?
EDIT
Requested to have example data.
product table
id name
1 product a
2 product b
3 product c
4 product d
version table
id product_id version_no
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 4 1
currency table
id product_id version_id currency
1 1 1 USD
2 2 2 USD
3 2 3 USD
4 3 4 USD
From this data, I should get this returned:
product_id product_name
3 product c
4 product d
This is because on Max versions of the following products
product_id version
1 1
2 2
3 2
4 1
Only product 3 and 4 have no entries on the currency table with these version_nos
Is this clear enough?
Use left join: and the where condition with null value will give you your desired result
select product_id,name
from
(select product_id, max(version_id) as vid
from versiontable
group by product_id) v
left join currencytable c on v.product_id=c.product_id and v.vid=c.version_id
left join procuttable p on p.id=v.product_id
where c.product_id is null and c.version_id is null
Related
I have two tables as follows:
docs
id
carid
name
1
1
doc1
2
1
doc2
3
2
doc3
4
1
doc4
5
5
doc5
cars
carid
parentid
name
1
4
car1
2
5
car2
3
4
car3
4
4
car4
5
5
car5
Question: I want to write a query in mysql where I can pass the carid in where clause and get all the rows from docs table where the parentid is same as that of the passed carid.
Desired Outcome If I pass carid=3 then the rows 1,2,4 from docs table should be returned as the parentid is 4 for carids 1,3,4.
Simillarly, If I pass carid=2 then the rows 3,5 from docs table should be returned as the parentid is 5 for carids 2.5.
You need to join the cars-table twice. First for the condition and second for the parent:
select d.*
from cars c
join cars p on p.parentid=c.parentid
join docs d on d.carid=p.carid
where c.carid=3
You're thinking about this a little wrong in the aspect of a relational database .. You SHOULD have 4 tables:
docs
doc_id
name
1
doc1
2
doc2
3
doc3
4
doc4
5
doc5
cars
car_id
name
1
car1
2
car2
3
car3
4
car4
5
car5
cars_to_docs
car_id
doc_id
1
1
1
2
1
4
2
3
5
5
parents_to_car
car_id
parent_id
1
4
2
5
3
4
4
4
5
5
Then you could simply use a basic JOIN
SELECT b.doc_id FROM test.docs a
LEFT JOIN test.cars_to_docs b
ON a.doc_id = b.car_id
LEFT JOIN test.parents_to_car c
ON c.car_id = b.car_id
LEFT JOIN test.cars d
ON c.car_id = d.car_id
WHERE c.parent_id = (SELECT parent_id FROM test.parents_to_car WHERE car_id = 3)
This will give you your output of 1,2,4
If I have a "SALES" table, with columns of SaleID, Product#, and CustomerName. and a PRODUCTS table with two columns product_ID and Name. The contains 5 differnt products. In the SALES table populates when a sale is made.
How would I query customer_name with only Product_ID of 1 and 2?
sales table
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 DAVE
2 2 DAVE
3 3 DAVE
4 1 TOM
5 2 TOM
6 1 JANE
7 1 MIKE
8 1 MIKE
9 3 MIKE
10 4 MARY
I would like a table result to be
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 TOM
2 2 TOM
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =1
INTERSECT
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =2
i want to count that how many times that each pair of SKU have been ordered together(have same orderid)
I have an order table of customers that has 2 column ORDERID and SKUID
for an example
ORDERID SKUID
1 1
1 2
1 3
2 1
2 2
2 4
3 1
3 4
i want a result table as
SKU1 SKU2 COUNT
1 2 2
1 3 1
1 4 2
2 3 1
2 4 1
3 4 0
thank you
You can do this with a self-join and aggregation:
select o1.sku, o2.sku, count(distinct o1.orderid) as numorders
from orders o1 join
orders o2
on o1.orderid = o2.orderid and o1.sku < o2.sku;
If you know that the skus are unique in each order, then you can use count(*) instead of count(distinct).
upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id
I have 2 tables
Table A is as follows:
ID NAME VALUE
1 abc 0
2 lmn 0
3 xyz 0
Table B is as follows:
ID SUB_GROUP VALUE
1 Category 1 10
1 Category 2 4
1 Category 3 8
1 Category 4 12
3 Category 1 6
3 Category 2 14
3 Category 3 0
3 Category 4 3
I want to UPDATE Table A by setting its VALUE column to the largest VALUE in Table B by matching the ID columns but only for the values in Table B in Category2 or Category 3
What might that MySQL query look like?
UPDATE tableA a
INNER JOIN
(
SELECT ID, MAX(Value) max_val
FROM tableB
WHERE SUB_GROUP IN ('Category 2','Category 3')
GROUP BY ID
) b ON a.ID = b.ID
SET a.VALUE = b.Max_Val
SQLFiddle Demo