Get the App
SLTechnology News&Howtos  ›  Database  › 

SQL queries are sorted by specified fields

Shulou Source: shulou.com Published: 2022-06-01 09:44:25 10月02日 Update

city_name is the field name, now press ("Beijing","Shanghai","Guangzhou") To sort out the data in sequence? Do not know what database you have, if it is Oracle:select count(city_name), city_name from tuan_info where source_type = 1 and city_name in("Beijing","Shanghai","Guangzhou") group by city_nameORDER BY INSTR ('Beijing, Shanghai, Guangzhou', city_name); ORDER BY CHARINDEX(city_name, 'Beijing, Shanghai, Guangzhou') if SQL Server

questioning

SQL Server, how to write oh, give a complete point statement, okay?

answer

select count(city_name), city_name from tuan_info where source_type = 1 and city_name in("Beijing","Shanghai","Guangzhou") group by city_nameORDER BY CHARINDEX(city_name, 'Beijing, Shanghai, Guangzhou') The SQL in front is the same ah. Just add an ORDER BY sort process.

questioning

[SQL] select count(city_name), city_name from tuan_info where source_type = 1 and city_name in("Beijing","Shanghai","Guangzhou") group by city_name ORDER BY CHARINDEX(city_name, 'Beijing, Shanghai, Guangzhou');[Err] 1305 - FUNCTION data_group.CHARINDEX does not exist

answer

What kind of database is this? CHARINDEX is a SQL Server function.

questioning

That, that's MYSQL, what should I do?

answer

MySQL changes a function. INSTRselect count(city_name), city_name from tuan_info where source_type = 1 and city_name in("Beijing","Shanghai","Guangzhou") group by city_nameORDER BY INSTR ('Beijing, Shanghai, Guangzhou', city_name)

Tags: Shanghai Beijing Guangzhou data function database field sort query no sequence just data query hour statement problem order processing prompt statistics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Shulou Technology Shulou Tech Info Docker