How to sort based on keyword first? - mysql

Here is a sample Database
First_name country
Andy US
Manu India
Paul Pakistan
Ramesh Pakistan
Rich India
So, what i want is to select all records from the above table and display according to name.
Like :-
I want to select person name to be display first whose country name is India and after US, Pakistan.
How can i perform this task in single SQL query ?
Update
I don't know how many Country are there.
Country the need to be display first will be input by the user.

Use a CASE statement to give each record a sortkey. 0 for a country match, 1 for a mismatch, so the desired country comes first.
select *
from mytable
order by case when country = #country then 0 else 1 end, first_name

May be something like this
Select * From Table1
Order By CASE WHEN country = 'INDIA' THEN 0
WHEN country = 'US' THEN 1
Esle 2
END;
Or You can use FIELD
Select * From Table1 Order By FIELD(country, 'India', 'US', 'Pakistan') ;

Use FIELD Function
Try this:
SELECT fitst_name, country
FROM tableA
ORDER BY FIELD(country, 'India', 'US', 'Pakistan'), fitst_name

Related

SQL query order by pattern

I am using this particular example
https://www.w3schools.com/sql/sql_orderby.asp
I want to get the list of customers ordered by country pattern.
For instance, Country starting with As should be first in number, followed by countries starting with Ge, followed by country starting with Fr.
If I need to order by using 1 pattern search in the country I can achieve using the following code :
SELECT * FROM Customers
ORDER BY (CASE WHEN Country LIKE 'Fr%' THEN 0
ELSE 1
END)ASC;
How can I add 2 more conditions to the ORDER BY clause ? Thank you.
You can add multiple CASE for your additional "conditions" by incrementing the number each time:
SELECT * FROM Customers
ORDER BY (CASE
WHEN Country LIKE 'As%' THEN 0
WHEN Country LIKE 'Ge%' THEN 1
WHEN Country LIKE 'Fr%' THEN 2
ELSE 3
END) ASC;

How to select column name and values both to display on a page?

I want to select a value from a row and want to display only such values where there is a value with column name
----------------
id name gender
---------------
1 Bob M
2 Anny
3 Harry M
so I want to display 2 Anny with Id and gender only, that means if I select 1 there should be id, name, gender.
I am unable to figure out how to write code statement
IF my understanding is correct, You wanted to Select the name and Gender only when you search the id? is that right?
If that so:
Please try this
SELECT id, gender FROM [your_table] WHERE id = 2
EDIT
As I have said on the comment section, Please see this as reference.
SELECT IIF(gender IS NULL , name , name & ' ' & gender) AS Info FROM [your_table]
You want to display 2 Anny with Id and gender only. concat() function will help you for this
select concat(id,' ',name) as name , gender from table_name where id=2

Mysql SELECT priority

My database is missing some values:
Name Surname Country Salary
John Walker USA 800
Walker Canada 1000
Peter Walker Canada 800
John Walker 900
Peter Farmer USA 1200
... ... ...
I want to SELECT Name and Surname and Country, if there are no values,
I want to SELECT by Name and Country, if there are no values,
I want to SELECT by Surname and Country.
I am using now:
SELECT Salary FROM table_name
WHERE (Name='InputFromPHP' AND Surname='InputFromPHP' AND Country='InputFromPHP')
OR (Name='InputFromPHP' AND Surname='InputFromPHP')
OR (Name='InputFromPHP' AND Country='InputFromPHP')
I want to give priority, so it will select exactly what was Input With PHP, but when there are several values in database it gives SQL error.
Thanks a million in advance.
You could use order by and limit:
SELECT Salary
FROM table_name
WHERE Name='InputFromPHP'
AND (Surname='InputFromPHP' OR Country='InputFromPHP')
ORDER BY CASE Surname WHEN 'InputFromPHP' THEN 0 ELSE 1 END,
CASE Country WHEN 'InputFromPHP' THEN 0 ELSE 1 END
LIMIT 1
Note that your where clause always required Name='InputFromPHP', so that can be taken out of the or clause.

MySQL Query to display records from table

I have a query.I have table with two columns country and state.I want to display columns in following format
Country State
----------- ---------
India Delhi
Bangalore
Kolkata
Mumbai
USA California
Florida
Las Vegas
Virginia
It means "India" just appear one time in country column and and repeated values would come as blank value in country column when i select country and state from table.
Thanks in advance
Presentation is usually if not always better done outside of SQL so I'd recommend doing this in whatever your presentation layer runs, but if it's a requirement for the query, you can do it using session variables;
SELECT Country, State FROM (
SELECT IF(Country=#country, '', Country) Country, State, #country := Country
FROM (SELECT Country, State FROM Table1 ORDER BY Country, State) dummy1,
(SELECT #country:='') dummy2
) dummy3;
An SQLfiddle to test with.
Just to show a (probably) better way, you can use this to get a list of states per country, and process it further in your presentation layer;
SELECT Country, GROUP_CONCAT(State) FROM Table1 GROUP BY Country;
Another SQLfiddle.
use pl/sql.Moreover your table would be voilating 5th normal form.

MySQL Multiple query with a bit of logic

I have a Users table with these values (stripped down for the example):
Name
City
County
StateProvince
Country
Continent
Strength
Endurance
Intelligence
I basically want a query that returns the highest stat by region for yourself. If you have the highest Strength (67) in the country, highest Endurance (59) in your City, and not the highest Intelligence anywhere, I'd like a 2 row result of:
('Strength', 67, 'Country', 'United States')
('Endurance', 59, 'City', 'Redmond')
Note that if you have the highest Strength in the country, you will also have the highest strength in the state/province, county, and city (but I'd like those not to be returned in the result). I can do this all using multiple SELECT queries, however, I'd like to know how to do this in one single query for efficiency (or if that's even a good idea?). I'd imagine the logic would go something like (pseudo code):
(SELECT FROM Users ORDERBY Strength WHERE Country = 'MyCountry' LIMIT 1) IF Name != 'Me' (SELECT FROM Users ORDERBY Strength WHERE StateProvince = 'MyState' LIMIT 1) ... and so on
Furthermore, the above would also need to work with Endurance and Intelligence. Thanks!
Check this link MySQL control-flow-functions
SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;