This question already has answers here:
MySQL ORDER BY IN()
(4 answers)
Closed 4 years ago.
I am trying to display the table using where condition on id column, but when I run the query the query, the table displays without where condition
Table: test
id name
1 aaa
2 bbb
3 ccc
I want to display the table like below :
Select * from test where id in ('2','3','1')
when I run the above query it is displayed as failing where condition.
1. aaa
2. bbb
3. ccc
try below query
select * from test
where id in (2,3,1)
order by
case id
when 2 then 1
when 3 then 2
when 1 then 3
end
here is the link for db-fiddle
Try this
SELECT
*
FROM
test
WHERE
id in ('2','3','1')
ORDER BY FIELD(id,'2','3','1');
Related
This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 7 years ago.
Alright, so I have a table of persons and each person belongs to a couple. I need to run a query that gets only the older member of each couple. Here is my schema:
id BIGINT
name VARCHAR(32)
couple_id VARCHAR(255)
birthdate TIMESTAMP
And the query I'm working on:
SELECT * FROM person
GROUP BY couple_id
HAVING birthdate > ???
The couple_id is a randomized string. Currently the output looks like this:
1 John AAA 1985/12/04
2 Jane AAA 1984/01/02
3 Christopher BBB 1991/07/07
4 Christina BBB 1992/08/20
5 Alex CCC 1995/02/07
6 Alexandra CCC 1996/11/12
I need a query that returns the rows John, Christina, and Alexandra.
One way to do it is using a derived table.
SELECT p1.*
FROM person p1
join (select couple_id, max(birthdate) as mxbrthdt
from person group by couple_id) p2
on p2.couple_id = p1.couple_id and p1.birthdate = p2.mxbrthdt
id - in_id - nat_id
1 1 1
2 1 3
3 3 3
4 2 1
Is it possible to select with mysql only the values in the above table which are the same across the 3 columns, ie return 1 and 3?
Or is this kind of filter only possible post query with php?
Thanks,
John
This simple query should work for you:
SELECT id
FROM your_table
WHERE id = in_id
AND nat_id = in_id
;
Check example at SQLFiddle: SQLFiddle Example
This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`
This question already has an answer here:
MySQL : Multiple row as comma separated single row
(1 answer)
Closed 9 years ago.
Here is my table information
Id Name
1 aaa
1 bbb
2 ccc
2 ddd
Ids are the repeated one. Now I want the result like
1 aaa,bbb
2 ccc,ddd
How can I do that in Mysql ?
Use GROUP_CONCAT()
select id, group_concat(name) as names
from your_table
group by id