Querying for multiple values - mysql

I am currently writing code that is supposed to query for several different records with multiple IDs. I have tried:
SELECT * FROM Items WHERE ID = ( 1, 2 )
SELECT * FROM Items WHERE ID = 1, 2
SELECT * FROM Items WHERE ID = '1, 2'
What am I doing wrong?

It would work just as well. What you're looking for information on is the "IN" operator of most SQL languages (MySQL example here).
what you're doing is providing a series of values as a set (right side of the IN operator) and then asking the database to bring back records whose column you're querying about (the left side of the IN operator) that have one of the values in the set.

use IN if you are comparing to multiple values.
SELECT * FROM Items WHERE ID IN (1, 2)
which is the same for
SELECT * FROM Items WHERE ID = 1 OR ID = 2
for this statement: "... query for several different records with multiple IDs", I think you want to search for a group which has multiple ID, if that's the case, the problem is called Relational Division.
SELECT groupColumn
FROM Items
WHERE ID IN (1, 2)
GROUP BY groupColumn
HAVING COUNT(*) = 2

What you need to type is
SELECT * FROM Items WHERE ID IN (1,2)
Note for string values inside the IN brackets use single quotes ('1','2')

Use IN like following
SELECT * FROM Items WHERE ID IN (1, 2);

SYNTAX:
SELECT * FROM TABLE WHERE ID IN (id1, id2, ..., idn)
Here in your case:
SELECT * FROM Items WHERE ID IN (1, 2);

Use this
SELECT * FROM Items WHERE ID = 1 or ID = 2;
or use short hand notation
SELECT * FROM Items WHERE ID IN (1,2);

use
select * from items where id in (1,2);

Related

Sql select where array in column

In my query I use join table category_attributes. Let's assume we have such rows:
category_id|attribute_id
1|1
1|2
1|3
I want to have the query which suites the two following needs. I have a variable (php) of allowed attribute_id's. If the array is subset of attribute_id then category_id should be selected, if not - no results.
First case:
select * from category_attributes where (1,2,3,4) in category_attributes.attribute_id
should give no results.
Second case
select * from category_attributes where (1,2,3) in category_attributes.attribute_id
should give all three rows (see dummy rows at the beginning).
So I would like to have reverse side of what standard SQL in does.
Solution
Step 1: Group the data by the field you want to check.
Step 2: Left join the list of required values with the records obtained in the previous step.
Step 3: Now we have a list with required values and corresponding values from the table. The second column will be equal to required value if it exist in the table and NULL otherwise.
Count null values in the right column. If it is equal to 0, then it means table contains all the required values. In that case return all records from the table. Otherwise there must be at least one required value is missing in the table. So, return no records.
Sample
Table "Data":
Required values:
10, 20, 50
Query:
SELECT *
FROM Data
WHERE (SELECT Count(*)
FROM (SELECT D.value
FROM (SELECT 10 AS value
UNION
SELECT 20 AS value
UNION
SELECT 50 AS value) T
LEFT JOIN (SELECT value
FROM Data
GROUP BY value) D
ON ( T.value = D.value )) J
WHERE value IS NULL) = 0;
You can use group by and having:
select ca.category_id
from category_attributes ca
where ca.attribute_id in (1, 2, 3, 4)
group by ca.category_id
having count(*) = 4; -- "4" is the size of the list
This assumes that the table has no duplicates (which is typical for attribute mapping tables). If that is a possibility, use:
having count(distinct ca.attribute_id) = 4
You can aggregate attribute_id into array and compare two array from php.
SELECT category_id FROM
(select category_id, group_concat(attribute_id) as attributes from category_attributes
order by attribute_id) t WHERE t.attributes = (1, 2, 3);
But you need to find another way to compare arrays or make sure that array is always sorted.

GROUP CONCAT not working in a subquery - mysql

I have a mysql query which I've written in order to grab a group of attributes in the database based on IDs, which relate to a specific group. I'm using OpenCart 2 for my site, and my subquery is as follows:
SELECT GROUP_CONCAT(attribute_id) AS attr_ids
FROM oc79_attribute
WHERE attribute_group_id = 13
Which returns:
44,45,46,47
When I write this within the query which I need to get the attribute names, I only get one result:
SELECT *
FROM oc79_attribute_description
WHERE attribute_id IN(SELECT GROUP_CONCAT(attribute_id) AS attr_ids
FROM oc79_attribute WHERE attribute_group_id = 13)
I only get the result from attribute_id 44, and not the others even though I know the records exist.
Is this the right way to approach this, or am I just missing something?
EDIT:
To clarify, if I write:
SELECT * FROM oc79_attribute_description WHERE attribute_id IN(44,45,46,47)
I get the correct result of 4 records.
Thanks in advance
There is no need for GROUP_CONCAT:
SELECT *
FROM oc79_attribute_description
WHERE attribute_id IN(SELECT attribute_id AS attr_ids
FROM oc79_attribute
WHERE attribute_group_id = 13);
See the difference between:
WHERE attribute_id IN ('44,45,46,47') -- one string
and
WHERE attribute_id IN ('44','45','46','47') -- multiple values
GROUP_CONCAT will return one string with , as separator, what you need is multiple values.
If the attribute table only has one row per attribute_id then you can use a join simple:-
SELECT oc79_attribute_description.*
FROM oc79_attribute_description
INNER JOIN oc79_attribute
ON oc79_attribute_description.attribute_id = oc79_attribute.attribute_id
AND oc79_attribute.attribute_group_id = 13
If it has multiples then you could use DISTINCT:-
SELECT DISTINCT oc79_attribute_description.*
FROM oc79_attribute_description
INNER JOIN oc79_attribute
ON oc79_attribute_description.attribute_id = oc79_attribute.attribute_id
AND oc79_attribute.attribute_group_id = 13
If you really wanted to search through a string of comma separated values (and I really would advise against it) then you could use FIND_IN_SET:-
SELECT *
FROM oc79_attribute_description
WHERE FIND_IN_SET(attribute_id, (SELECT GROUP_CONCAT(attribute_id) AS attr_ids
FROM oc79_attribute WHERE attribute_group_id = 13))

Why am i getting "Subquery returns more than 1 row"

Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.

Select 2 different columns from 2 different tables

I'm trying to select 2 different columns (newsID from the table news and movID from the table movies) so that I can use mysql_num_rows to grab the items in those conditions.
I tried this with the code below, but it is not working. How can I fix it?
$queryy="SELECT newsID FROM ".PREFIX."news WHERE published='1'";
$queryy="UNION (SELECT movID FROM ".PREFIX."movies WHERE activated='2')";
$all=safe_query($queryy);
$gesamt=mysql_num_rows($all);
You're overwriting the variable with the second assignment. Do it all in one string assignment:
$queryy = "SELECT newsID FROM ".PREFIX."news WHERE published='1'
UNION (SELECT movID FROM ".PREFIX."movies WHERE activated='2')";

MySQL - Check if array values with the same id exist

I've got table with two fields: id, numbers
I want to check if the same array values (etc 1,2,5,6) already exist in table, but they have to have the same 1nd row number (id).
I've tried with this sql query, but this one doesn't check if there is the same id:
SELECT id, numbers FROM `table` WHERE numbers IN (1,2,5,6) AND id = id
I know that "id = id" doesn't work, but I posted it so you'll know what I mean.
SELECT id, COUNT(*) num_count, GROUP_CONCAT(numbers ORDER BY numbers) all_numbers
FROM `table`
WHERE numbers IN (1, 2, 5, 6)
GROUP BY id
If you only want to see the ones that have all 4 numbers, add:
HAVING num_count = 4
If you want the IDs that have all and only those 4 numbers, use:
SELECT id, COUNT(*) all_count, SUM(numbers IN (1, 2, 5, 6)) in_count
FROM `table`
GROUP BY id
HAVING all_count = in_count