I have a database with those fields:
id (int)
name (text)
priority (int)
I want to show values from database where id = (e.g) 13 and sort them by priority.
E.G: The field with id 13 and priority 1 will be first from the other field with priority 2.
How can i do it?
SELECT id,name,priority from table_name WHERE id=13 ORDER BY priority ASC;
Your query:
SELECT `id`,`name`,`priority` FROM table_name WHERE `id`=13 ORDER BY `priority` ASC;
The ordering can be managed with the field function:
SELECT `id`,`name`,`priority` FROM table_name
order by FIELD(priority,"13","2")
Watch out for practical issues: YOu have to manually maintain this order list (or create some structure to maintain this).
Related
I have one such sql:
select name from A where id in (23,24,22,23)
When I run it in Navicat, the result only have one result of 23.
My question is, how to keep the number and order of the query results remains the same as (23,24,22,23).
If you want to maintain the order of the result then use order by clause like
select name from A
where id in (23,24,22)
order by id;
Again, assuming that id is a primary key column in your table A then there will be only one row with id = 23. How do you expect the same row to get repeated automatically unless you make it explicit by using a UNION ALL
If you really really want to fetch the records like this, you can use field function to get 23,24,22 and order by this sort:
select name from A where id in (23,24,22) order by field(id, '23,24,22')
then use union all get another 23:
(select name from A where id in (23,24,22) order by field(id, '23,24,22'))
union all
select name from A where id = 23
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
I have a simple query, but I would like to see the results in a specific way. I would like to see 'N/A' at the top of the result, without having to result to "Case When Then"
Select *
From Ordertype
Results:
Car21
Car34
Bus42
N/A
Thanks,
There are no 'overrides' for ORDER BY, if you want the specific order you're asking for, you'll have to use the CASE:
SELECT type
FROM OrderType
ORDER BY
CASE
WHEN type = 'N/A' THEN 1
ELSE 2
END
,type
If you want an arbitrary order that is not tied directly to the structured of a column (alphabetical/numerical) but rather to it's importance which only you know in your head it can be useful to add a Rank column to your table.
Column1 Rank
Car21
Car34 2
Bus42 1
N/A 99
then you can do
select Column1
from Table
order by rank desc, column1
This will put highly ranked items first then low ranked items, then when rows don't have a rank it will sort them alphabetically by column1
You can try this:
SELECT * FROM ordertype ORDER BY ID DESC
to see the newest ones 1st
I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?
You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.
assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);
To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1
Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.
I've got a problem in mysql, I have a table of ID and nubmer ... somthing like that
ID -------- number
3 -------- 340
1 -------- 10
12 -------- 23
And now I would like to selecet by ID, let's say ID=3 and is there a way of saying what is the position of this row in sorted table? In this case, ID = 3 would have first position, cuase of highest value in number. ID=12 would have second position ... and so on.
set #row_number:=0;
select * from
(select ID, #row_number:=#row_number+1 from your_table order by number desc)
as row_to_return
where ID=3;
The above query can be change to replace ID to anything you need.
However, for simple usage, ORDER BY number DESC is better and optimized.
try this query if usefull.
SELECT ID,number FROM table_name ORDER BY number DESC;
Thanks.
If you're returning the resultset via PHP, you can use PHP to determine the position of the value '3' within the array for the ID field by using array_search
Or change your SQL as follows:
select
#rownumber:=1+#rownumber as 'rownumber',
id_field, number_field
from yourtable, (SELECT #rownumber:=0) rownumber order by number_field DESC
This will add an additional column 'rownumber' which provides the number of each row