I have this sql output that gives me different row for the same person based on their location. I wanted one line per person and three columns with a Y if they lived there.
Select name, Paris, London, NYC from location
Name Paris London NYC
John y
John y
John y
I want this
Name Paris London NYc
John y y y
you can use max function :
select name, max(paris), max(london), max(nyc) from location
group by name
SELECT name,
IF(SUM(IF(Paris='y',1,0)>0,'y','') as Paris,
IF(SUM(IF(London='y',1,0)>0,'y','') as London,
IF(SUM(IF(NYC='y',1,0)>0,'y','') as NYC
FROM location
GROUP BY name
Side-note, the database is not designed optimally! 3 tables with Name, Cities and Location with proper joins would be much more efficient.
Use Subselects like:
Select name,
(SELECT Paris FROM location WHERE name = a.name),
(SELECT London FROM location WHERE name = a.name)
FROM location a
This way you make a Select in an Select, and link the subselect to the name of the overlaying select.
Some of the other users told you to use aggregations.
In the most database systems an aggregation query is less efficient than a subquery, so you should use aggregations with care.
Related
I am stuck with some requirement,
this my 1st query
SELECT price FROM resdential WHERE (CITY LIKE '$Location') AND ( Bedrooms='$Bedrooms' AND Bathsroom='$Bathroom')
I want to search for apartment when city is London and city is any other(Dorchester in case),
(prioritising London is essential)
now every time I get results only from London , and I need combined result, combined London and Dorchester result or any other city results together, need help
yeah you can say use * but, first I want to search for London den any other city
Do you mean this?:
SELECT price FROM resdential WHERE (CITY IN ('$Location')) AND ( Bedrooms='$Bedrooms' AND Bathsroom='$Bathroom')
$Location would be a list of cities where London is included.
I would have a list of cities near london on hand in your script(or from a database) search for city="london" or city="city2" or city="city3" then handle the prioritization of London inside your logic. For example
$prioList=aray();
$nonPrioList=array();
$results=mysqli_query("SELECT City, Price FROM resdential WHERE CITY IN '$selectedCity' ")
while($row=mysqli_fetch_assoc($results)){
if($row['City']=="London"){
array_push($prioList,$row);
}else{
array_push($nonPrioList,$row);
}
}
That should give you two arrays, one with your london results and the other with your non london results.
SELECT City, Price FROM resdential WHERE ( Bedrooms=2 AND bathrooms=2) ORDER BY (CITY= 'London') DESC , CITY
this really worked for me,
bdw thanks for all your help, I really appreciate , You helped me too go in right direction
If we assume that I already have a query that presented me with the data I want in a very simple way for a mailer: Name, Address, City, State, Zip. But if the file's around 20k, there are a decent amount of people who have the exact same address.
Obviously it's simple to do a GROUP BY Address to remove any duplicates. But the problem I'm running into is not being able to update the names to include both people for a mailer.
If this is two rows in the original data:
Name Address City State Zip
Jerry Seinfeld 129 West 81st Street New York NY 10024
Elaine Benes 129 West 81st Street New York NY 10024
I would like the query output to result in one row where the name looks like this:
Name Address City State Zip
Jerry Seinfeld & Elaine Benes 129 West 81st Street New York NY 10024
And if we want to get fancier, it would be great if they also had the same last name to include something for that in the script so that if both of their last names were "Seinfeld," the output would be:
Name Address City State Zip
Jerry & Elaine Seinfeld 129 West 81st Street New York NY 10024
So generally, I'm just looking for some SELECT *-type query that households these addresses but also includes something to update the names. Thanks!
Below Query would work well, Self tested:
SELECT
IF(COUNT(T1.Address) = (SELECT COUNT(DISTINCT T2.Last) FROM t1 T2 WHERE T2.Address = T1.Address), GROUP_CONCAT(T1.First," ",T1.Last SEPARATOR ' & '), CONCAT(GROUP_CONCAT(T1.First SEPARATOR ' & '), ' ',T1.Last)) Name
FROM t1 T1
GROUP BY T1.Address
GROUP_CONCAT()
Examples here: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
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;
I have a table that looks like this:
target_id || country_code
5-----------||-------US----
5-----------||-------CA---
2----------||-------FR----
3----------||-------SP----
3----------||-------FR----
And another table that looks like this:
target_id || region_name
5-----------||---North America
2-----------||-----France------
3-----------||-----Some Europe
As you can see, table 2 contains locations and target_ids, while table 1 contains where these locations are targeted. In the case of North America, it is targeted to 5, which belongs to Canada and US. France, on the other hand has a target_id of 2, and Some Europe a target_id of 3, which contains France again and Spain.
What I would like to do via MySQL, is to get a table of target_id, country_code, country_name but only for countries. This means, only to the target_ids of table 1 that are in only one row (for example, we know that FR is a country because number 2 is only in FR, and we know that 3 represents a region because it has both Spain and France associated). Is this possible to do via MySQL or will I need two queries and PHP in the middle?
Thanks!
SELECT t1.target_id, t1.country_code, t2.region_name
FROM table1 t1
JOIN table t2
ON t1.target_id = t2.target_id
WHERE (SELECT COUNT(*) FROM table1 t3 WHERE t3.target_id = t1.target_id) = 1
table1 is the one with the country codes, table2 is the one with the the region names.
i dont understand the problem with returning multiple rows:
here is my table BBC:
name region area population gdp
Afghanistan South Asia 652225 26000000
Albania Europe 28728 3200000 6656000000
Algeria Middle East 2400000 32900000 75012000000
Andorra Europe 468 64000
Angola Africa 1250000 14500000 14935000000
etc.............................
question:
List the name and region of countries
in the regions containing 'India',
'Iran'.
this is my statement:
select name from bbc where region = (select region from bbc where name='India' or name='Iran')
it returns:
sql: errorSubquery returns more than 1 row
whats wrong with my statement? the answer should be in the form of a select statement within a select statement
thank you!
This is because you are trying to compare region to a table of values. Instead, try using in:
select name
from bbc
where region in
(select region from bbc where name='India' or name='Iran')
You might have slightly different syntax and it'll work:
SELECT name
FROM bbc
WHERE region IN
(
SELECT region FROM bbc WHERE name='India' OR name='Iran'
)
The only difference being that instead of equals (=), we use IN.
The reason your previous one failed is because to use equals, you compare one value with one other value. What you were accidentally doing is comparing one value with multiple values (the "SubQuery returns more than one row"). The change here is saying where region is within the results returned from the sub query.
select name,region from bbc where region IN (select region from bbc where name IN('India','Iran'))