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.
Related
I have a database that looks like this
leads
name zip city county state
Bill 84058
Susan 90001
FullUSZipCodes
ZipCode City County State
84058 Orem Utah Utah
90001 Los Angeles South California
As you can see, in the leads database the city, count and state are empty, i'm trying to merge the data so that the first table would look like this:
leads
name zip city county state
Bill 84058 Orem Utah Utah
Susan 90001 Los Angeles South California
This is the first query I tried:
UPDATE leads SET leads.county = FullUSZipCodes.County WHERE leads.zip = FullUSZipCodes.ZipCode
which didn't work, and here is the 2nd query:
UPDATE leads INNER JOIN FullUSZipCodes ON leads.zip = FullUSZipCodes.ZipCode SET leads.county = FullUSZipCodes.County, leads.city = FullUSZipCodes.City, leads.state = FullUSZipCodes.State
Your second query looks like it should work, but I would write it slightly differently:
UPDATE leads t1
INNER JOIN FullUSZipCodes t2
ON t1.zip = t2.ZipCode
SET
t1.city = t2.City,
t1.county = t2.County,
t1.state = t2.State
WHERE
t1.city IS NULL OR t1.county IS NULL OR t1.state IS NULL;
If the leads table has a lot of records, and performance is a concern, you can try adding a WHERE clause to the update query which targets only those records which are missing one or more pieces of address information. I think there is nothing wrong with always updating all three columns together, because typically this information tends to be grouped together.
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.
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;
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 :)
I think my issue with databases stems from not knowing the correct terminology to help me find an answer myself so I'll explain a generic version of what I'm doing and hopefully you can point some tutorials my way or give me some terms to check into.
Let's use an example of an employee directory.
Each employee can have multiple locations, multiple job duties which pull from a separate table. Example tables & some data, let's just focus on the multiple locations.
employees
Main employee data
- id (ex: 400)
- first (ex: John)
- last (ex: Doe)
locations
Unique list of locations
- id (ex: 3000)
- title (ex: FakeCo, LLC)
map_employees_locations
Tie ANY number of locations to an employee
- id
- employee_id (ex: 400)
- location_id (ex: 3000)
I'm struggling with the logic of how a single query would return something like this:
John
Doe
FakeCo, LLC
AnotherCo, LLC
It seems I would have to run a query to get the employee data, then within a nested query, grab locations associated with the employee id, etc... If there was only one location per employee, it would be a simple join, I just don't know how to handle the multiples.
Let me know if I'm way off, I'm just struggling.
You would join all of the tables together like this
select e.id,e.first,e.last,l.id,l.title
from employees e
inner join map_employees_locations el
on el.employee_id = e.id
inner join locations l
on el.location_id = l.id
where e.first = 'John'
AND e.last = 'Doe'
This would return data like this:
e.id e.first e.last l.id l.title
------------------------------------------------
1 John Doe 1 FakeCo, LLC
1 John Doe 2 AnotherCo, LLC
If you want only one line per employee you should maybe use group concat
select id, e.last, e.first
group_concat(l.title separator ',' ) as locations
from employee e
join location l on l.employee_id = e.id
group by e.id
Not sure about the syntax cos i'm more aware of postgres but this should do the job.