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`
Related
Need to select data if condition first true then check user id exist, if condition not true then check condition two then check user id not exist
Technologies used: Codigniter - Mysqli
SELECT *
FROM `tablename`
WHERE `user_id` = '1'
OR `user_id` IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3')
AND `user_id` NOT IN('35')
OR (CASE WHEN except_friends = 1 THEN FIND_IN_SET('1', friends_list), FALSE END CASE WHEN selected_friends = 1 THEN NOT FIND_IN_SET('1', friends_list), FALSE END) ORDER BY `id` desc
Mysql syntax error
I think there is syntax error in your query. try this one
SELECT * FROM tablename
WHERE user_id = '1'
OR user_id IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3')
AND user_id NOT IN('35')
OR
(CASE
WHEN except_friends = 1
THEN NOT FIND_IN_SET('1', friends_list)
WHEN selected_friends = 1
THEN NOT FIND_IN_SET('1', friends_list)
END) ORDER BY id desc
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
To simplify the question I am only using a few field in my test table example
Master db
Id Description type cost
'1', 'Test1', '2', '100'
'2', 'Test2', '2', '100'
'3', 'Test3', '3', '100'
'4', 'Test4', '4', '100'
Labels db
ID Name Masterid
'1', 'Label1', '1'
'2', 'Label1', '2'
'3', 'Label2', '1'
'4', 'Label3', '1'
I would like to count all ID's and make summary for the cost field for all records in master containing label1 and label2 from labels
My Query
Select count(Distinct m.id) as andtall , sum(m.cost) as cost
from
master m
join labels l ON l.Masterid=m.id and l.name in ('Label1','Label2')
Since I am using Distinct in count that result will be correct, but Cost is wrong it's containg 3 records not 2.
'2', '300' I would like it to return 200 since only 2 records from master table should be returned.
Try this
SELECT
count( m.id ) as andtall,
sum( m.cost ) as cost
FROM
master m
JOIN (
SELECT
Masterid
FROM
labels l
WHERE
l.name in ('Label1', 'Label2')
GROUP BY master_id ) l ON l.Masterid = m.id
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().
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.