How do i copy data from one table to another table? - mysql

I have 2 tables, country and division with the ff schemas:
country division
----------- ------------
countrycode divisioncode
contryname countrycode
divisionname
I want to copy data from the country table into the division table where countrycode goes into divisioncode and countrycode in division table and countryname going into divisionname
For ex, US - United States goes into the division table as US, US, United States.

Use INSERT INTO ... SELECT
Query
INSERT INTO division(divisioncode, countrycode, divisionname)
SELECT countrycode, countrycode, countryname
FROM country;

insert into division (divisioncode, countrycode, divisionname)
select t1.countrycode, t1.countrycode, t1.countryname
from country t1
See: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

Related

Mysql Unique / distinct rows based on two or more columns

I have a table where I have the following columns - lets call location table
city, state, zipcode, lat, lng
The problem is that every city and state can multiple zip codes e.g.
belleuve, wa, 98004
bellevue, wa, 98006
Similarly a city name can be also present in some other state e.g.
bellevue, TN, 05156
How do I select a distinct city e.g. Bellevue for each state. Basically the result should show both Bellevue, WA and Bellevue, TN but for WA only one occurance of Bellevue.
Grouping by the city and state should work here:
SELECT City, State
FROM yourTable
GROUP BY City, State;
We also could use DISTINCT:
SELECT DISTINCT City, State
FROM yourTable;

SELECT column1 FROM table WHERE column2 is greater than column1

I want to display all values of a column that is greater than its equivalent value in another row
Like for example,
Display country name WHERE population is greater than country name (i.e. Andorra)
SELECT name FROM country
WHERE population > population (of Andorra)
You can use subquery for this, e.g.:
SELECT name
FROM country
WHERE population > (
SELECT population
FROM country
WHERE name = 'Andorra'
);
Please note that this query will return an error if
there is no record with name country name 'Andorra' or
there is more than one record with country name 'Andorra'
SELECT name FROM country
WHERE population > (SELECT population FROM country WHERE name = 'Andorra')
You think something like this?

How to sort based on keyword first?

Here is a sample Database
First_name country
Andy US
Manu India
Paul Pakistan
Ramesh Pakistan
Rich India
So, what i want is to select all records from the above table and display according to name.
Like :-
I want to select person name to be display first whose country name is India and after US, Pakistan.
How can i perform this task in single SQL query ?
Update
I don't know how many Country are there.
Country the need to be display first will be input by the user.
Use a CASE statement to give each record a sortkey. 0 for a country match, 1 for a mismatch, so the desired country comes first.
select *
from mytable
order by case when country = #country then 0 else 1 end, first_name
May be something like this
Select * From Table1
Order By CASE WHEN country = 'INDIA' THEN 0
WHEN country = 'US' THEN 1
Esle 2
END;
Or You can use FIELD
Select * From Table1 Order By FIELD(country, 'India', 'US', 'Pakistan') ;
Use FIELD Function
Try this:
SELECT fitst_name, country
FROM tableA
ORDER BY FIELD(country, 'India', 'US', 'Pakistan'), fitst_name

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.

Grouping Mysql query contacts table by city

an easy one, its long time i do not use mysql queries so maybe someone can help me out with this nooby question, i have a contacts table, id, name, and city.. i want to get all the contacts listed as follows.
city 1
--------
contact1
contact2
contact3
city 2
--------
contact4
contact7
contact10
city 3
--------
contact5
contact6
contact8
I do not want to use any additional php coding, just get the sql result like this
city 1
-> contact 1
-> contact 2
-> contact 3
city 2
-> contact 4
-> contact 7
-> contact 10
...
then fill the result in a php object and do like this:
foreach (cities as city)
{
// code here
}
Thanks in advance
Use GROUP_CONCAT
SELECT city, GROUP_CONCAT(name)
FROM contacts
GROUP BY city
You just need to GROUP BY city column and use GROUP_CONCAT function in SELECT to get comma seprated list of names for each city.
SELECT city, GROUP_CONCAT(name) AS name
FROM contacts
GROUP BY city;