I have two tables States and Cities.
In teststates table: there are data regarding states of different countries
The code column has id for states associated with it
In Cities table:
there is region column which has same id as of code column in teststates table.
Requirement:
I want the Code in mysql to fetch id from the teststates table column and replace it into region column in testcities table as I want only two columns which are city name and region in testcities table. Please help me!
select a.name,b.region from teststates a,testcities b where a.country = b.country
This will return City Name and Region Name columns joining on Country name from both tables.
Related
I have two tables: Employees and Pharmacies.
In table Employees I have a column name Pharmacy which tells me what pharmacy an employee works at, and also a column with the names of the employees.
In table Pharmacies I have a column of pharmacies and I want to add a new one named Number_of_employees which to contain the number employees from the corresponding pharmacy where he works.
Any ideas how to do this ?
DON'T ADD the column Number_of_employees
you can return Number_of_employees value by query
Select Pharmacies.*, count(Employees.Pharmacy) as Number_of_employees from Pharmacies
join Employees on Pharmacies.id = Employees.Pharmacy
group by Employees.Pharmacy
I have a MYSQL table of groups of people organised by country, region, sub region and city.
When visitor join a group, he select a city and we automatically add him in the parents groups "sub-region", "region, "country".
For example: John selected London. So he will be added in groups London, Greater London, England, UK.
We get a parent-child table like this:
http://prntscr.com/3kui69
I need to extract all the rows for the city groups. How to recognise a city groups? It is the only rows where its ID is not in id_parent field of other rows.
Yes! City groups rows can't be parents of other groups. So we can't find city groups id in id_parent fields.
Now that we know his, how can I extract the city groups rows with SQL language? It is too complicate for me.
Thanks in advance.
Please try using this query:
select * from table
where id not in (select id_parent from table);
Here is an example: http://sqlfiddle.com/#!2/7de26/1
i need to create a field in my table in order to put a title to all my properties for sale. I am not a programmer but i studied a little mysql, and I cant find a solution to this.
I need to fill in a field in a mysql table using information from different tables, let me explain.
I have a table named "products" where the locality and category are defined with a number (id), then I have another 2 tables named "localities" and "categories" where a string is assigned to the id number of the "products" table, ex:
in products table i have a product with locality id n. 10 and category id n.17, in localities table the id n.10 has the value "locality 1" and in categories table the id n.17 has the value "houses for sale".
Well, i need to update the products table and set the "title" field using information from the other tables and create a title string for ALL ROWS using the info from the other tables, in order to have titles similar to the following one:
"Houses for sale in locality 10"
and for example
"Apartments for sale in locality 23"
"Land for sale in locality 34"
etc.
Please note that the word "in" is not part of any table so I need to insert it in the middle in some way. I hope i have been clear enough, if not please ask. Thanks.
You can do what you want using update with join. Here is an example:
update products p join
localities l
on p.localityid = l.locality id join
categories c
on p.categoryid = c.categoryid
set p.title = concat(c.value, ' in locality ', l.value);
Is there is a way MySQL to get the column names given the value of the column?
For example I have STATES table and CITY table which have 'NAME' as column name.
Say I have 'New York' in both STATES table and CITY table. I want a query which will return the column names of SATES and CITY give 'New York'.
My Expected Output could be like
value column Table
----- ------ -----
New York NAME STATES
New York NAME CITY
Above is just an example. In most of the cases i only know the column values and i dont know the Column names or the corresponding table Names. Generally I need not know on the diffrent random schema's. I am building a question answersing system that will work with multiple databases.
When querying, to help prevent ambiguity, always qualify the fields with alias.column such as
select states.name,
city.name as CityName
from
city
join states
on city.stateabbrev = states.stateabbrev
where
city.name = 'New York'
you must be specified the column name to select data,
get column name,you can run SQL query:
desc tablename
or using like ResultSetMetaData(in java)
I'm using Geonames database for a hotel booking website. The database has two tables, one for countries, and one for cities with over 3 mil. entries. If I try to get all the cities for a specific country the query is too slow. I think is because I don't have any index defined.
The countries table has the following fields:
iso_alpha2 (country code)
name
continent
population
The cities table has the following fields:
name
asciiname
alternate_names
country
The "country" field from the cities table relates to "iso_alpha2" field in the countries table.
How can I speed up the query?
P.S. I'm using MySQL.
You need to add an index on the field that you use in the WHERE clause (in your case it seems to be the country field).
Edit: one more thing - if you have multiple conditions in the WHERE clause you need to add an index that contains all the fields used in that clause (having separate indexes on the fields won't work). However in your case I believe that the index on the country field should do.
For this query, you'd only need the cities table:
select name from cities where country = 'US'
This query would benefit from an index on country.