EDIT:That was fast. The reason why I have this is because the table is a pivot table between 2 tables one has "id" as primary key and the other "type" primary key
Hello.
I want the following:
Find only find "id" where "type" is 1 AND 2 AND 3
This is not working:
SELECT * FROM `table` WHERE `type` = 1 AND `type` = 2 AND `type` = 3;
The SELECT statment should only return one row (id = 1)
Table
id type
1 1
1 2
1 3
2 1
2 2
3 3
If you only want to know the Id then add the keyword Distinct and just select the id,
where there are records for the three different types ...
Select Distinct id
FROM `table` t
WHERE Exists (Select * From Table Where id = t.id and Type = '1')
And Exists (Select * From Table Where id = t.id and Type = '2')
And Exists (Select * From Table Where id = t.id and Type = '3')
If you want to see the Id and the type then add the type to the select,
Select Distinct id, Type
FROM `table` t
WHERE Exists (Select * From Table Where id = t.id and Type = '1')
And Exists (Select * From Table Where id = t.id and Type = '2')
And Exists (Select * From Table Where id = t.id and Type = '3')
if you want to see every row that has that id, then leave out the distinct
Select id, Type
FROM `table` t
WHERE Exists (Select * From Table Where id = t.id and Type = '1')
And Exists (Select * From Table Where id = t.id and Type = '2')
And Exists (Select * From Table Where id = t.id and Type = '3')
A row can't have a column with a value 1 AND 2 AND 3 at the same time. It's like asking you if you're "20 and 21 and 22 years old". You can only be one of them.
You'd want to do something akin to:
SELECT id WHERE type = 1
INTERSECT
SELECT id WHERE type = 2
INTERSECT
SELECT id WHERE type = 3
but MySQL doesn't support INTERSECT, so you have to do it by hand:
SELECT id FROM table WHERE
id IN (SELECT id FROM table WHERE
id IN (SELECT id FROM table WHERE type = 3)
AND type = 2)
AND type = 1
You can do it using intersection of sets
SELECT id FROM table WHERE type = 1
INTERSECT
SELECT id FROM table WHERE type = 2
INTERSECT
SELECT id FROM table WHERE type = 3
I am thinking if there is also simplier query but now I have no idea
Your search conditions are incompatible. Field 'type' can't be at the same time equal to 1, 2 and 3. Given the absence of INTERSECT in MySQL, you can join the same table two times:
SELECT id FROM
Table t1 JOIN Table t2 ON (t1.id=t2.id) JOIN Table t3 ON (t3.id=t2.id)
WHERE t1.type=1 AND t2.type=2 AND t3.type=3
This will build a cartesian product of rows with the same id and retain only those which have all three types you want.
Learning the basics of SQL can help you a lot. There is a lot of information around. (including MySQL docs)
If I understand you correctly, couldn't you just add LIMIT 1 at the end of your query?
Your selecting for the impossible. For each row, type can not be three things at once.
You're doing something wrong. You should not have multiple rows with the same id. It destroys the point of having one. Your id column should be a primary key that auto increments.
I think you should go back and reexamine your schema. Or at least add more detail about what you're trying to do.
Related
I need to get an entry from mysql with a given id but the version should be maximum.
I have a table of entries. All entries have id and version( these two are the composite keys of the table). I need to get all entries with unique id and max version.. How do I do it efficiently.
1. do I read all and pick the max for each id.
2. do i create a column with name is_latest, index on it and set it to true when new entry is put while updating older entries to false.
3. something else. :)
This should be noted that most of the calls will be read(>99%).
let the table have n entries with id 1 and versions from 1 to n
id version name ...
1 1 abcd ...
1 2 abcd ...
1 3 defg ...
I would like to get the last entry
1 3 defg ...
In mysql you could use order by and limit 1 for a single id
select *
from my_table
order by version desc
limit 1
of for several id
use inner join for max
select *
from my_table m
inner join (
select id, max(version) max_ver
from my_table
group by id
) t on t.max_ver = m.version and t.id = m.id
I think you are looking for the MAX() function.
SELECT MAX(version) from my_table where id = your_id
You can have more information here : https://sql.sh/fonctions/agregation/max (French website)
Example here : https://www.db-fiddle.com/f/tJHG1JT6RJLGJJoamYHXzQ/1
See this db-fiddle: https://www.db-fiddle.com/f/wMxb7S5tCGVA8gsXLHUHS3/0
SELECT t2.id, t2.version, t1.name
FROM my_table t1
JOIN (
SELECT id, max(version) version FROM my_table GROUP BY id
) t2 ON t1.id = t2.id AND t1.version = t2.version
Hi everyone I am trying to
SELECT * rows WHERE id = (for example) 123.
Also, I want to select the column favorite_number from row 123 and select all WHERE id = favorite_number of id = 123. How can I do that?
Here is what I have so far:
SELECT * FROM users WHERE (id = :id) OR (id = :id AND get favorite_number from that row and search for it as ID);
Example: SELECT all rows with ID = 123. Also, if that row's column favorite_number IS NOT NULL AND does not = 0, select favorite_number (in this example, let's say the number is 456) and then search the same table: WHERE id = 456.
How can I do this all in one search query? I hope this is clear enough, thanks!
Select * From Users Where Id = 123 Or Id in
(Select favorite_number From Users Where Id = 123 And favorite_number Is Not Null And favorite_number <> 0)
seems you are looking for both the condition on the same rows but for different column
in this way you should add an AND condtion extending where
SELECT *
FROM users
WHERE id = :id
AND favorite_number = :id ;
You can use a selfjoin;
SELECT * -- You should always explicitly select the columns rather than *
FROM users us
LEFT JOIN users uf
ON uf.Id = us.favorite_number
WHERE us.id = 123
Obviously you can change to an inner join if required and add more where conditions (or join conditions).
I assume you want 1 or 2 rows and you can do it with UNION:
select * from tablename
where id = 123
union all
select * from tablename
where id = (
select favorite_number from tablename where id = 123
)
In case favorite_number is null or 0 then nothing will be fetched by the 2nd query.
Let us consider we I have a table as follows
I need to select the records with same "group_id" s where at least anyone record's "type" is equal to 1.
The expected result set must be like
This should work:
select * from table where group_id in
(
select group_id from table where type = 1
)
You can also try join instead of in
select a.* from table a
join
(
select group_id from table where type = 1
) b on b.group_id = a.group_id
example table:
id | value
----------
1 | abc
1 | cb3
1 | dsf
2 | sss
2 | d3
So if the input is "cb3" I want to get all rows with id 1, if the input is "sss" I want to get all rows with id 2. Can this be done with one query instead of two ? (first query is find id and second query bring rows for found id). Also, would a one query approach be faster ?
Thank you in advance.
you could try this :
SELECT *
FROM TABLE my_table
WHERE id IN (SELECT id
FROM TABLE my_table
WHERE value = input
)
Try the following, where you replace 'sss' with the value you are searching for:
select * from table t1
where t1.id in (select id from table t2 where value = 'sss')
Note that value seems not to be a key, such that you might get two different groups of ids in your result. That's also the reason why I proposed an t1.id IN (select id ... rather than an t1.id = (select id ....
You can solve it using inner join also.
SELECT S.*
FROM dataset AS S
INNER JOIN dataset AS T
ON S.id = T.id
WHERE T.value = 'cb3';
I have 2 tables in MySQL, the first one has 2 columns: ID and name, the second has 3 columns: firstTableId (foreign key on the first table), key, value.
I have the following rows in table 1:
1,Bob
2,Alice
3,Fred
I have the following rows in table 2:
1,age,20
1,gender,male
2,age,20
2,gender,female
3,age,18
3,gender,male
I would like to write a select query using only the last 2 columns on the second table (key and value) that returns only Bob form the first table, but I can't seem to figure it out.
Essentially I want to select from the first table all rows where, in the second table, we have key=age and value=20 for one row, and key=gender and value=male in another row. Can anyone point me in the right direction ? Manipulating table structure is not preferred as this is a simplified example and both "key" and "value" columns in the second table can be pretty much anything, it's not actually limited to "age" and "gender".
Thanks in advance.
You can do this with a self join like this:
select
*
from
table1 t1
inner join table2 age on t1.id = age.id
inner join table2 gender on t1.id = gender.id
where
(age.`key` = 'age' and age.value = 20)
and
(gender.`key` = 'gender' and gender.value = 'male')
An additional tactic you may want to try is a PIVOT query. Mysql doesnt have anything native to support pivot's, but there are several examples of how to do them.
You can see it working in this fiddle
Use two IN clauses (or two EXISTS clauses).
select *
from table1
where id in (select firstTableId from table2 where key = 'age' and value = '20')
and id in (select firstTableId from table2 where key = 'gender' and value = 'male');
With EXISTS:
select *
from table1
where exists (select * from table2 where key = 'age' and value = '20' and firstTableId = table1.firstTableId)
and exists (select * from table2 where key = 'gender' and value = 'male' and firstTableId = table1.firstTableId);