I have a table with the following design:
ID OPT_1 OPT_2 OPT_3 A_ID
1 3 4 3 1
2 5 2 1 1
3 1 2 2 1
I want to select all OPT's for A_ID 1, but when I run the query, I don't know the A_ID, I only know the OPT's.
So how can I insert a variable to get A_ID for the options I want?
for instance:
SELECT * FROM table
WHERE ((OPT_1 = 1 OR OPT_1 = 5)
AND (OPT_2 = 4 OR OPT_2 = 2)
AND (OPT_3 = 3 OR OPT_3 = 2)
AND A_ID = $X=$X)
Thanks,
Possibly best way to do it is to split it up a bit.
SELECT *
FROM table
WHERE A_ID in (
SELECT A_ID
FROM table
WHERE (OPT_1 = 1 OR OPT_1 = 5)
union
SELECT A_ID
FROM table
WHERE (OPT_2 = 4 OR OPT_2 = 2)
)
Related
I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here
I have a table that stores values for attributes associated to products.
row_id product_id attribute_id value
1 1 1 a
2 1 2 b
3 2 1 d
4 2 2 e
How do i write a select to get the value for attribute_id=2 only for products that have value="a" for attribute_id=1?
Thanks,
Have a nice day
Maybe this is what you want?
select *
from your_table
where attribute_id = 2
and product_id in (
select product_id
from your_table
where attribute_id = 1 and value = 'a'
)
With your sample data the row with row_id = 2 would be returned.
I've got the following associative table between packages and products (simplified):
package_id product_id count
1 1 6
1 2 1
1 3 1
2 1 6
2 2 1
3 1 6
4 1 8
4 2 1
I'm trying to work out how to create an query which is able to select specific package_id's which contain exactly the products and their counts I supply. So if I'd be trying to find the package that contains: (product_id = 1 AND count = 6) AND (product_id = 2 AND count = 1), it should only return package_id 2 and not the others, because those contain other products and / or other counts.
I'd be happy to work this out in my code (PHP) instead of SQL, but since I'm trying to get to the bottom of queries, I'd like to know how this is done.
This is called Relational Division
SELECT a.package_ID
FROM tableName a
WHERE (a.product_ID = 1 AND a.count = 6) OR
(a.product_ID = 2 AND a.count = 1)
GROUP BY a.package_ID
HAVING COUNT(*) = 2 AND
COUNT(*) = (SELECT COUNT(*) FROM tableName WHERE package_ID = a.package_ID)
SQLFiddle Demo
OR
SELECT package_ID
FROM tableName
WHERE (product_ID, `count`) in ((1, 6), (2, 1))
GROUP BY package_ID
HAVING COUNT(DISTINCT product_ID, `count`) = 2 AND
COUNT(*) = (SELECT COUNT(*) FROM tableName WHERE package_ID = a.package_ID)
SQLFiddle Demo
I am writing a query to grab the items that a specific user_id was the first to use. Here is some sample data -
item_id used_user_id date_used
1 1 2012-08-25
1 2 2012-08-26
1 3 2012-08-27
2 2 2012-08-27
3 1 2012-08-27
4 1 2012-08-21
4 3 2012-08-24
5 3 2012-08-23
query
select item_id as inner_item_id, ( select used_user_id
from test
where test.item_id = inner_item_id
order by date_used asc
limit 1 ) as first_to_use_it
from test
where used_user_id = 1
group by item_id
It returns the correct values
inner_item_id first_to_use_it
1 1
3 1
4 1
but the query is VERY slow on a giant table. Is there a certain index that I can use or a better query that I can write?
i can't get exactly what you mean because in your inner query you have sorted it by their used_user_id and and on your outer query you have filtered it also by their userid. Why not do this directly?
SELECT DISTINCT item_id AS inner_item_id,
used_user_id AS first_to_use_it
FROM test
WHERE used_user_id = 1
UPDATE 1
SELECT b.item_id,
b.used_user_id AS first_to_use_it
FROM
(
SELECT item_ID, MIN(date_used) minDate
FROM tableName
GROUP BY item_ID
) a
INNER JOIN tableName b
ON a.item_ID = b.item_ID AND
a.minDate = b.date_used
WHERE b.used_user_id = 1
x_Id | y_Id | z_Id
----- |----- |-----
1 | 1 | 1
2 | 1 | 1
3 | 1 | 1
4 | 1 | 1
5 | 1 | 1
1 | 2 | 3
I am relatively new at programming and I cant figure out this MySql query. I need to select x_Id only where ((y_Id = 1 AND z_Id = 1) AND (y_Id = 2 AND z_Id = 3)).
Therefore, using these numbers as an example the only thing that should be selected is (x_Id =) 1.
**All of these columns are in the same table
The closest I have come is by using this query:
SELECT
*
FROM
`relationships`
WHERE
y_id = 1 AND
z_id = 1
UNION
SELECT
*
FROM
`relationships`
WHERE
z_id = 3 AND
y_id = 2
However, this returns all the x_ids and x_id = 1 again as a duplicate.
**I am using sqlPro and MySql 5
no need to Union.
Updated after seeing comments:
select
*
from relationships T1
INNER JOIN relationshipsT2 on t1.x_Id = t2.x_Id where
((T1.y_Id = 1 AND T1.z_Id = 1) AND (T2.y_Id = 2 AND T2.zz_Id= 3))
also you can only return x_Id instead of *
If you are only interested in the x_id value you can use the query above, but just add DISTINCT and project only the x_id value.
Example:
SELECT
DISTINCT x_id
FROM
`relationships`
WHERE
y_id = 1 AND z_id = 1
UNION
SELECT
DISTINCT x_id
FROM
`relationships`
WHERE
z_id = 3 AND y_id = 2
There are few other way how to do it, which are even easier such as use OR in the WHERE clause.
Updated after seeing comments:
Using an aggregate SUM() you can total up the number of conditions met per value of x_id. If the total is > 1, both conditions are met somewhere in the table.
SELECT DISTINCT x_id FROM (
SELECT
x_id,
SUMCASE WHEN (y_id = 1 AND z_id = 1) OR (z_id = 3 AND y_id = 2) THEN 1 ELSE 0 END) AS hasboth
FROM relationships
GROUP BY x_id
HAVING hasboth > 1
) subq