combining query for query optimisation - mysql

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

Related

What are the MySQL queries for the following tasks? Are my solutions and line of thinking correct so far? Thank you very much

This is my very first question on Quora. Thanks for any recommendations , solutions and remarks. I was not provided with many specifics and data, this is a assignments where one must use his or her imagination to fill in particular data and variables. What counts is the correct logic, approach and covering possibilities. Your help is highly appreciated! Thank you!
Country and Continent table
Required Visual Result for Question 3
Questions:
Write a query that would select all countries with GDP of more than 1 000 000 000 USD
Write a query that would return all countries in Europe (specifically) with GDP of more than 1 000 000 000 USD
Write a query that lists all continents with GDP per continent (as the sum of the GDP of all countries). Each country belong to one continent only.
For what result should look like - please resort to "Required Visual Result for Question 3" image.
My Solutions:
select * from countries where GDP > 1000000000
select * from countries where continent_id = 2 and GDP>1000000000;
select sum(GDP) from countries where continent_id = 4;
However, here in 3) I can only have the GDP sum displayed, and do not know how to have the continent's name on the left side as well. Please, if possible, assist with having the continent's name displayed and then right next to it and on the right handside - the relevant GDP sum.
Welcome!
Your image shows the tables as country and continent but your queries refer to countries? So which is it please?
On the basis your image is correct but the queries are wrong then number 3 would be as below:
With no 3 your currently only going to get the some for the continent with the ID of 4.
select sum(GDP) from country where continent_id = 4;
So what you want to do is remove the WHERE and the GROUP BY continent_id to give you 1 result per continent.
select sum(GDP) from country where 1 GROUP BY continent_id
No to get the continent name included in your results you can use the JOIN syntax.
In this instance you want all your records from country and just the records from continent that match your join condition which will be the continent_id from country and the id from the continent table.
SELECT
`continent`.`name`, sum(`GDP`)
FROM `country`
LEFT JOIN `continent`
ON `country`.`continent_id` = `continent`.`id`
GROUP BY `continent_id`
ORDER BY `continent`.`name` ASC;
That should give you the results 1 per continent as required.
I've specified the table names as its clearer how to target specific columns from each table.

Putting one line per name using SQL

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.

how to have a Count when using Groupby?

I have got a column name country in which there are 3 entries which are shown below. In each countries i have set of people working in it in a different column. I want a count query which can count how people are working in each country in a single query.
country
________
India
America
China
try this:
SELECT country,COUNT(*)
FROM table
GROUP BY country;

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.

help with subquery! returns more than 1 row

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'))