I have a query like this:
SELECT title,id FROM table1 WHERE id IN ('2','7','4','10')
The result set is ordered by id by default, but I need it in the exact order of numbers in above set.
How can I achieve this?
The FIELD() function should be able to do this:
SELECT
title, id
FROM
table1
WHERE
id IN ('2', '7', '4', '10')
ORDER BY FIELD(id, '2', '7', '4', '10')
See also MySQL sort after argument in IN().
Related
Need help to make query.
I have table like this:
kli, akt, mes
'2', '2', '201209'
'2', '2', '201210'
'3', '3', '201211'
And I need a result table:
kli, akt, mes
'2', '2', '201209'
'4', '4', '201210'
'7', '7', '201211'
result tables sum by field mes, current and all previous rows
try the below query
SELECT SUM(b.kli),SUM(b.akt),a.mes FROM tableName a INNER JOIN tableName b ON a.mes >= b.mes GROUP BY a.mes
SELECT * FROM TJU.`group`;
group_id, name
'1', 'x'
'2', 'y'
'3', 'z'
'4', 'a'
'5', 'b'
I want re arrange position of data in mysql using select statement
expect output
group_id, name
'1', 'x'
'2', 'y'
'3', 'z'
'5', 'b'
'4', 'a'
please suggest me how to implement this so that I can get record like this
try a query like this:
select * from TJU.`group` order by FIELD(group_id,1,2,3,5,4(;;
to change id without name
select
find_in_set (id,'1,2,3,5,4'),`name`
from TJU.`group`
I want to get last record of each day in mysql.Location<id, date, place_id> table has multiple entries on each day. This Location table has place_id and time at which place_id is inserted.
Also taking consider if place_id is not present then return second last record which has place_id. In following table for NULL, '2016-04-06 18:52:06' record we are returning '13664', '2016-04-06 12:57:30', which is second last record on '2016-04-06' (6th March) and has place_id.
One more thing, on single day, there would be more place_id, see the following table..
id || place_id || date
'1', '47', '2016-04-05 18:09:37'
'2', '48', '2016-04-05 12:09:37'
'3', '13664', '2016-04-06 12:57:30'
'4', '9553', '2016-04-08 10:09:37'
'5', NULL, '2016-04-06 18:52:06'
'6', '9537', '2016-04-07 03:34:24'
'7', '9537', '2016-04-07 03:34:24'
'8', '656', '2016-04-07 05:34:24'
'9', '7', '2016-04-07 05:34:57'
When I run following query it returns following result
Query I run the following query but it is giving me wrong result
`Location<id, place_id, date>`
select L1.place_id, L1.date from
Location1 L1
Left join
Location1 L2
on
Date(L1.date) = Date(L2.date)
And
L1.date < L2.date
where
L2.date is null
group by L1.date;
Result I want:
id....place_id ........date
'1', '47', '2016-04-05 18:09:37'
'3', '13664', '2016-04-06 12:57:30'
'4', '9553', '2016-04-08 10:09:37'
'9', '7', '2016-04-07 05:34:57'
You may give it a try:
SELECT
L.id,
L.place_id,
L.date
FROM Location L
INNER JOIN
(
SELECT
MAX(date) max_time
FROM Location
GROUP BY Date(`date`)
) AS t
ON L.date = t.max_time
SQL FIDDLE DEMO
SQL FIDDLE DEMO2
[Based on your expected output]
Can you try with the following query:
SELECT * FROM `Location` GROUP BY DATE(`date`) ORDER BY `date` DESC
What this query does is group the rows by descending date and show a row for each date.
Get the last record:
SELECT * FROM `Location` ORDER BY `date` LIMIT 1;
Get the last record that doesn't have a null as a value:
SELECT * FROM `Location` WHERE place_id IS NOT NULL ORDER BY `date` LIMIT 1;
Get records for all the places which are not null:
SELECT * FROM `Location` WHERE place_id IS NOT NULL GROUP BY `place_id` ORDER BY `date` DESC
Would this work?
select place_id, max(date) as MaxDate
from foo
where place_id is not NULL
group by place_id
I have a list of localized results by country. I would first like to get the result of the Effective Country X first and then the others in the same. Is that possible?
If I set a where "anno_country" = 1 ... it excludes the results of other countries. I would like something like "order by country = 3" ...
Currently, this is my MySQL query :
SELECT DISTINCT *
FROM (`annonce`)
JOIN possede USING (`anno_id`)
JOIN annonceur USING (`ann_id`)
JOIN lang_pays USING (`pays_id`)
JOIN cat_lang USING (`cat_id`)
WHERE
`cat_id` IN ('4', '9', '5', '426', '6', '435', '7', '3', '8', '2')
AND
`anno_active` = 1
AND
`anno_mode` = 1
AND
`cat_lang`.`lang_id` = '3'
AND
`lang_pays`.`lang_id` = '3'
ORDER BY `anno_id` desc
Do you have an idea ?
You can use this:
ORDER BY (country != 3), anno_id DESC
this will show rows with country = 3 at the top, ordered by anno_id desc, and then all rows with country!=3 at the bottom, ordered by anno_id desc.
SELECT * FROMyourtableORDER BY (country= 'X') DESC,country
This will order by country x first then other countries.
I have the following problem
I have 3 table (all of the are used utf8 / utf8_general_ci encoding)
movies, channels, i also have 3 table movie_channels which
is just a combination of the other two with just 2 fields: movie_id,channel_id
here is my channels table (code,name)
'1', 'ОРТ'
'2', 'ТК Спорт'
'3', 'ТК ТНВ'
'4', 'НТВ'
'5', 'НТВ+'
'6', 'TSN'
here is my movie_channels table (movie _id, channel_id) channel_id references code field in channels table
'19', '2'
'19', '6'
'95', '1'
'95', '4'
'96', '1'
'96', '4'
'97', '1'
'97', '4'
'98', '1'
'98', '4'
'99', '1'
'99', '4'
'100', '1'
'100', '4'
don't mind quotes on id values. they are all ints of course, not chars, it's just pasting issue
for each movie i need to display comma separated list of channels
i used mysql group_concat
select t.movie_id,( select group_concat(c.name) from
movie_channels mc
join channels c on mc.channel_id=c.code
where mc.movie_id = t.movie_id
order by code desc )as audio_channel from movies t
but I dont like the order of concat for movie_id #19 i need the above sql to display TSN,ТК Спорт but it keeps returning me ТК Спорт,TSN . I tried to use order by code desc with no luck ,tried order by char_length(asc) with no success
any ideas ?
If for every movie you want to list all the channels that are showing that movie, you need something like this:
SELECT
mc.movie_id,
GROUP_CONCAT(c.name) AS channels
FROM movie_channels AS mc
JOIN channels AS c
ON c.code = mc.channel_id
GROUP BY mc.movie_id
ORDER BY c.code DESC;
You can add a ORDER BY clause inside the GROUP_CONCAT() aggregate to adjust the ordering of the grouped string.