Mysql SELECT priority - mysql

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.

Related

the sum of fields values from a column with a condition

I am quite new to mySQL and need help.
I have a database like this (values are example):
name |region |population|
----------------------------------
Cuba |Carribean |10 |
Ukraine|Eastern Europe|15 |
Belarus|Eastern Europe|9 |
Haiti |Carribean |3 |
I want to find total population of the region (e.g. all population from Eastern Europe) and print as a table the region name and its total population.
But I have no idea how to find a sum of field value from one column but under condition from another column.
How to to do this query?
SELECT region, SUM(population) AS population
FROM table
GROUP BY region
Try below query:
select name , region , sum(population)
from region_table // table_name
group by name , region
May be this will help you.

SQL: Repeated records by grouping some columns

I have a data like,
ID Name ItemA ItemB ItemC
OXZ234 Adam 4 4 5
OXZ234 Adam 1 2 3
OXZ345 Tarzen 6 7 8
OXDER2 William 9 8 2
OXDER2 William 0 8 0
I need to find how much of food each person eats. For example by referring first two records I can say, Adam of ID OXZ234 ate ItemA-5, ItemB-6 and ItemC-8. But for small amount of data this kind of manual calculation is affordable. I have a million data records like this. So initially I need to find the records which is having same ID and name but only items count differing.
I have tried the query to find duplicate records by grouping all columns like below,
select ID,Name,ItemA,ItemB,ItemC, COUNT(*)
from DATA_REFRESH
group by ID,Name,ItemA,ItemB,ItemC
having COUNT(*) > 1
But Now I have to identify records having items columns differed.
So the expected output is like,
OXZ234 Adam 2
OXDER2 William 2
OXZ345 Tarzen 1
Any suggestion would be helpful!
You want SUM
select ID,
Name,
sum(ItemA) as ItA,
sum(ItemB) as ItB,
sum(ItemC) as ItC,
count(ID) as Occurrences -- Counts the number of entries per person
from DATA_REFRESH
group by ID,Name
having count(ID) >1 -- restricts this so only those with more than one entry appear
Hi, You can have a simple query without having clause,
select ID,Name,COUNT(*)
from DATA_REFRESH
group by ID,Name order by COUNT(*) desc ;
Simply try like this,
select ID,Name,COUNT(*)
from Sample_Check
group by ID,Name
having COUNT(*) > 1

How to sort based on keyword first?

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

How to select every row N times in MySql table?

I have a MySQL table like below.
ID NAME SURNAME
1 Joe Black
2 Mary Peterson
3 Bob Marley
4 Andy Murray
...
I want to write a SELECT command which will return every row N times.
Example for N = 3.
SELECT NAME, SURNAME blah-blah (3 Times ) FROM Person
NAME SURNAME
Joe Black
Joe Black
Joe Black
Mary Peterson
Mary Peterson
Mary Peterson
Bob Marley
Bob Marley
Bob Marley
Andy Murray
Andy Murray
Thank you.
You could use UNION ALL to get the result you want, but you'd better not do such job with SQL.
SELECT * FROM
(
SELECT NAME, SURNAME FROM Person
UNION ALL
SELECT NAME, SURNAME FROM Person
UNION ALL
SELECT NAME, SURNAME FROM Person
) A ORDER BY NAME
I won't suggest sql to achieve this, a much simpler way would be (assuming PHP) :
$result = $mysqli->query("SELECT NAME, SURNAME blah-blah (3 Times ) FROM Person");
while($result_set = $result->fetch_array())
{
for($i=0;$i<4;$i++)
{
echo $result_set['name']." ".$result_set['sur_name']."\n";
}
}
This would give you a better control on the result set as well as, you can have more dynamic ways of handling, how the data is displayed on the page,
for. eg, if u want a particular name to be printed only once, a simple if comparison would do, which is not possible via sql!!
SELECT NAME, SURNAME FROM Person, (SELECT * FROM Person limit 3) count_table;
The dumb manual way would be:
SELECT NAME, SURNAME FROM Person
UNION ALL
SELECT NAME, SURNAME FROM Person
UNION ALL
SELECT NAME, SURNAME FROM Person
Another way is to have a table with numbers in a single column running up to the maximum and doing a cross join with it WHERE number <= n
what about go through looping it in stored procedure like that :
delimiter //
CREATE PROCEDURE ntimes()
BEGIN
DECLARE i INT DEFAULT 0;
SET i = 0;
WHILE i < 3 DO
SELECT `Code`, `Rank` FROM Table1;
SET i = i + 1;
END WHILE;
END;
//
DELIMITER ;
Here you just change 3 to what number of repeats you want , and you dont need mass of unions .

SQL Query to provide me with the total number

I have a table with 3 columns
Column 0: auto inc index
Column 1: Person's Gener
Column 2: The STATE the person lives in (US States and Puerto Rico)
I want to run a query that tells me how many MEN are in each state
so the output would list all 50 states (in 1 column) and the second would be a number to determine the number of MEN in that state
Alaska 1000
New York 85000
(the figures above aren't accurate but i'm illustrating what I am looking for)
Thanks
You need to use a GROUP BY clause and the COUNT aggregate function.
SELECT State, COUNT(*) AS NumberOfMen
FROM your_table
WHERE Gender = 'M'
GROUP BY State
try this
select STATE, count(*) as num from table_name where gender ='MALE' GROUP BY STATE;