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
Related
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
I want to join one to many table with single row on many table by limit 1 and order by create date
tbl_cart :
id fullname
1 myname1
2 myname2
3 myname3
tbl_cart_status:
id cart_id status created_at
1 1 33 2018-09-20
2 1 34 2018-09-23
3 2 34 2018-09-21
4 1 100 2018-09-25
5 2 35 2018-09-29
How can i get output with sql like this:
I want to get lastest status of my cart by ordered with created_at column
myname cart_id status created_at
myname1 1 100 2018-09-25
myname2 2 35 2018-09-29
Think filtering for this type of query:
select c.name, cs.*
from tbl_cart c join
tbl_cart_status cs
on c.id = cs.cart_id
where cs.created_at = (select max(cs2.created_at)
from tbl_cart_status cs2
where cs2.cart_id = cs.cart_id
);
I have two tables t1 and t2 with the same structure
id INT
userid INT
date DATETIME
The first table contains my data, while the second table is kind of helper table which contains rows for 10 fix dates and userid = -1
What i need is a SELECT which gives me all rows from t1 with userid=X joined(merged) with all rows from t2 which date is not already in the result of t1.
Pseudo code
SELECT id, date
FROM t1, t2
WHERE (t1.userid=:id OR t2.userid=-1) AND t2.date NOT IN t1.date
Sample:
t1:
id userid date
1 1 2015-12-01
2 1 2015-12-02
3 1 2015-12-03
4 2 2015-12-01
5 2 2015-12-02
t2:
id userid date
1 -1 2015-12-01
2 -1 2015-12-02
3 -1 2015-12-03
4 -1 2015-12-04
5 -1 2015-12-05
Expected output for userid=1:
1 1 2015-12-01
2 1 2015-12-02
3 1 2015-12-03
4 -1 2015-12-04
5 -1 2015-12-05
Thanks for your help
I'll use a union select for doing this.
SELECT
id, date
FROM
t1
WHERE
t1.id=:id
UNION ALL
(SELECT
id, date
FROM
t2
WHERE
t2.id=-1
AND t2.date NOT IN (SELECT date FROM t1 WHERE t1.userid=:id))
i have a table named iview:
gpreq_id a_id m_id rcv_qty
1 100 4 0
2 100 4 1
3 100 5 4
4 101 4 1
5 101 4 10
6 101 4 1
how can i select this that the m_id in the a_id's has the highest gpreq_id?
like:
gpreq_id a_id m_id rcv_qty
2 100 4 1
3 100 5 4
6 101 4 1
First find max value for each a_id, m_id pair and then join to iview:
select i.*
from iview as i
inner join (
select a_id, m_id, max(gpreq_id) as max_gpreq_id
from iview
group by a_id, m_id
) as mi on (mi.max_gpreq_id = i.pgreq_ie)
Try something like
SELECT i1.*
FROM iview as i1
WHERE i1.gpreq_id IN (SELECT MAX(gpreq_id)
FROM iview as i2
GROUP BY i2.a_id, i2.m_id)
Here is the SQL FIDDLE
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.