I'm trying to select all user_id's from the following table (excerpt) where product_id '3' is not associated with the user_id at all.
user_id product_id status terms_id
100 3 1 10
100 22 0 0
100 402 0 20
101 22 1 10
101 68 1 0
101 120 1 20
201 22 0 0
201 3 1 10
In this example, only user_id 101 should be selected because it doesn't have product_id 3 at all. Each user_id can have multiple entries.
I've tried the following, but it incorrectly selects all the user_id's
SELECT distinct user_id FROM table WHERE product_id <> 3
How could I accomplish this? The actual table has more than 3.5 million rows.
Thanks!
You can use a NOT EXISTS subquery to check that the user has no associated row with product_id = 3:
SELECT DISTINCT user_id
FROM `table` t1
WHERE NOT EXISTS (SELECT * FROM `table` t2 WHERE t2.user_id = t1.user_id AND t2.product_id = 3)
Output
101
Demo on dbfiddle
An alternate solution is to GROUP BY user_id and to assert that the count of rows with product_id = 3 is 0:
SELECT user_id
FROM `table`
GROUP BY user_id
HAVING SUM(product_id = 3) = 0
Demo on dbfiddle
Related
i'm stuck and trying to figure out how to do the right WHERE query.
This is the table:
post_id property_id property_value_id property_value_custom
77 2 3 NULL
79 1 1 NULL
79 2 2 NULL
79 2 3 NULL
79 4 NULL 111
80 3 4 NULL
I want to select the post, WHERE property_value_id = 3 AND property_value_custom = "111".
The result should be post_id 79. How to query that?
You can group by post_id and set the condition in the HAVING clause:
select post_id
from tablename
group by post_id
having sum(property_value_id = 3) > 0
and sum(property_value_custom = 111) > 0
or:
having max(property_value_id = 3) = 1
and max(property_value_custom = 111) = 1
ID item_ID parent_ID count
================================
1 11 2 5
2 12 2 6
3 13 3 2
4 14 3 3
5 15 2 7
6 16 1 3
SELECT * FROM relations ORDER BY count DESC
The row that should be returns are 2,4 and 6 because they have the highest count for their parent_ID
how do i change the query to accomplish this?
The inner select gets the highest count for each parent_ID. If you join against that, it filters out the relevant records
select t1.*
from your_table t1
join
(
select parent_ID, max(count) as mcount
from your_table
group by parent_ID
) t2 on t1.parent_ID = t2.parent_ID
and t1.count = t2.mcount
I want the minimum price from Table 2 to be filled in price column of Table 1 for a particular id.
Table 1
pid price
111 0
222 0
333 0
Table 2
pid price
111 100
111 200
222 120
222 90
333 200
333 150
Expected output: Table 1
pid price
111 100
222 90
333 150
You would do something like:
UPDATE Table1 t
SET t.price = (SELECT MIN(t2.price) FROM Table2 t2 WHERE t2.pid = t.pid);
this is query to get lowest price from table2
(SELECT price FROM table2 WHERE price = ( SELECT MIN(price) FROM table2 ) ) now you can update the table 1 ( update table1 set price="result you got from above query" where id=given id)
I have a relation table with this data. What I want is a query which returns an a_id to which every connected status equals 1.
So in this case, 6 would be the value returned.
a_id b_id status
4 757 0
4 758 0
4 761 0
5 757 1
5 758 0
5 761 1
6 757 1
6 761 1
6 758 1
MySQL 5.5
SELECT DISTINCT a_id
FROM relation_table
WHERE a_id NOT IN (SELECT a_id FROM relation_table WHERE status != 1);
Try this
SELECT DISTINCT a_id
FROM table1
WHERE a_id NOT IN
(SELECT a_id
FROM table1
WHERE status != 1);
SQL Fiddle
SELECT DISTINCT a_id
FROM table_name
WHERE a_id NOT IN (SELECT a_id FROM table_name WHERE status = 0);
Reference:
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct
This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.