i want to bring default country default city default town in every db grid in my admin panel
these are not drop down these are db grid
i have three table
country table
id(int),name(varchar),is_default enum('1','0')
city table
id(int),country_id(int),name(varchar),is_default enum('1','0')
town table
id(int),countr_id(int),city_id(int),name(varchar),is_default enum('1','0')
i have 3 link in my admin panel
Country list
City List
Town List
Country List
- when i open country list it will bring list but default country will come first
country default
USA yes (its coming at first line because its is_dfault=1)
Germany No
thats ok
Select * from country where is_default='1'
City List
When i open that list
City list will come with own county name, but default country will come at first line
and default city will be first in this default country in city list page
Select city.*,country.name as country
left join country on country.id=city.id
order by country.is_default,city.is_default asc
thats wrong sql i know
example
city name country default
new jersey USA yes (this city coming at first line because its is_default=1)
chicago USA no
koln Germany no (Germany cities starting after USA cities because country is default=1)
Town List
is_default value 1 town will come at first with own city towns at first line
example
town name city name country default
usa town newjersey USA 1 (this town coming at first line because its is_default=1)
after usa city towns
germany town1 koln GERMANY 0 (Germany or other country cities start after USA towns)
i think my problem is with order (order by is_default ) or i need diffirent sql with other selections
i tried some codes but countries coming mix and others city and towns list too
waiting your helps.
thanks
I have no idea whether i got what you mean, and I try to give an answer.
I think what mix the countries or other column is wrong join condition.
Your sql here:
Select city.*,country.name as country
left join country on country.id=city.id
order by country.is_default,city.is_default asc
Above is what you post, Because it is not complete, I guess your join condition "on country.id=city.id" is wrong. If you wanna combine the two table, on its contry id, you should
SELECT city.*, county.name AS country_name from
country LEFT JOIN city ON country.id = city.country_id
ORDER BY country.is_default, city.is_default DESC
Because "is_default" is 1, I use "DESC", then default rows whill pop first.
I hope that will be helpful for you :)
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
I have the following three tables Location, Country and CountryAlias. I want to find the locations country. Means I want to search location_name from location table against Country and CountryAlias and find the country code.
But while do like query. I am getting wrong output.
location
location_name
Bangalore, India
India
Chennai, India
Country
code name
IN India
IO British Indian Ocean Territory
CountryAlias
code alias
IN Bharth
IN Hindustan
Try Query
SELECT code from Country
LEFT JOIN CountryAlias ON Country.code = CountryAlias.code
where Country.`name` LIKE '%Bangalore, India%' or CountryAlias.alias LIKE '%Bangalore, India%'
O/P = NuLL
SELECT code from Country
LEFT JOIN CountryAlias ON Country.code = CountryAlias.code
where Country.`name` LIKE '%India%' or CountryAlias.alias LIKE '%India%'
O/P = IN and IO
Both wrong, Is there any perfect solution. I am using Innodb engine for tables.
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.
I have a world db. The table below was generated with:
SELECT *
FROM geolocations
WHERE city = 'Santa Cruz'
The table contains multiple entries for each city, it once contained a Zip Code field.
I want to delete duplicate entries for cities. I tried using:
CREATE TABLE tmp
SELECT DISTINCT city, region
FROM geolocations
The previous statement eliminates the duplicate entries, but how do I get id and other fields to copy to new table?
-----------------------------------------------------------------------
id Country Region City Latitutde Longitude
-----------------------------------------------------------------------
4683 US CA Santa Cruz 37.0447998047 -122.1020965576
5748 US CA Santa Cruz 36.9712982178 -121.9875030518
9506 US CA Santa Cruz 37.0101013184 -122.0324020386
11205 US CA Santa Cruz 37.0344009399 -121.9796981812
11379 US CA Santa Cruz 36.9898986816 -122.0603027344
13146 US CA Santa Cruz 37.0101013184 -122.0324020386
14362 US CA Santa Cruz 37.0101013184 -122.0324020386
30055 BO 03 Santa Cruz -12.2833003998 -66.2500000000
31760 ES 59 Santa Cruz 42.6666984558 -2.3499999046
39477 AR 22 Santa Cruz -27.6667003632 -64.2667007446
-----------------------------------------------------------------------
As Marvo said those are not duplicate rows perse but some fields duplicated. The best approach could be to redesign your database and break that table into two -at least-. This could serve you as a starting point:
First create a table that contains different-uniques- cities:
CREATE TABLE city SELECT DISTINCT city FROM db
Add a primary key to that table:
ALTER TABLE city ADD id_city INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
Create a table that contains all your records and the id of the corresponding city that each record belongs to:
CREATE TABLE records SELECT db.id,db.country, db.region, city.id_city, db.lat,db.long FROM db INNER JOIN city ON db.city = city.city
You can make another one for cities asocciated with countries if you wish.
This query worked I wanted to select distinct city, region combinations and group it with its associated data in the new table.
CREATE TABLE clean_gls SELECT * FROM geolocations GROUP BY city, region;
IN MSSQL it's this"
SELECT distinct City, Region
INTO tmp
FROM geolocations
WHERE City = 'Santa Cruz'
In MYSQL, you can use select into table
Although I havent tested it, something like this should work:
INSERT INTO tmp(City, Region)
SELECT distinct City, Region
FROM geolocations WHERE WHERE City = 'Santa Cruz'
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'))