SQL query to get state,then city and address and zip - mysql

I'm trying to get data from database in this format:
state
|__city
| |__address
| |__zip
I want all unique states and cities with all addresses and zip codes concatenated.
Here's what I've tried:
SELECT
state,
GROUP_CONCAT('(',address,';)') AS address,
GROUP_CONCAT('(',zip,';)') AS zip
FROM location
WHERE
sername='ABC#gmail.com'
GROUP BY state,city
UPDATE
There is problem with concatenation. I want the data in this format:
state city address zip
Karnataka BANGALORE (sdbsbd);(dsdsds); NULL
and I'm getting data as
state city address zip
Karnataka BANGALORE (sdbsbd);,(dsdsds); NULL
I don't want the comma in the address:
;,

I think you need the SEPERATOR argument in the GROUP_CONCAT() function.
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT state, GROUP_CONCAT('(',address,';)' SEPARATOR '') as
address,GROUP_CONCAT('(',zip,';)' SEPARATOR '') as zip FROM location Where
username='ABC#gmail.com' GROUP BY state,city"

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;

MySQL Householding: Group Duplicate Addresses And Update Names to Include Both People

If we assume that I already have a query that presented me with the data I want in a very simple way for a mailer: Name, Address, City, State, Zip. But if the file's around 20k, there are a decent amount of people who have the exact same address.
Obviously it's simple to do a GROUP BY Address to remove any duplicates. But the problem I'm running into is not being able to update the names to include both people for a mailer.
If this is two rows in the original data:
Name Address City State Zip
Jerry Seinfeld 129 West 81st Street New York NY 10024
Elaine Benes 129 West 81st Street New York NY 10024
I would like the query output to result in one row where the name looks like this:
Name Address City State Zip
Jerry Seinfeld & Elaine Benes 129 West 81st Street New York NY 10024
And if we want to get fancier, it would be great if they also had the same last name to include something for that in the script so that if both of their last names were "Seinfeld," the output would be:
Name Address City State Zip
Jerry & Elaine Seinfeld 129 West 81st Street New York NY 10024
So generally, I'm just looking for some SELECT *-type query that households these addresses but also includes something to update the names. Thanks!
Below Query would work well, Self tested:
SELECT
IF(COUNT(T1.Address) = (SELECT COUNT(DISTINCT T2.Last) FROM t1 T2 WHERE T2.Address = T1.Address), GROUP_CONCAT(T1.First," ",T1.Last SEPARATOR ' & '), CONCAT(GROUP_CONCAT(T1.First SEPARATOR ' & '), ' ',T1.Last)) Name
FROM t1 T1
GROUP BY T1.Address
GROUP_CONCAT()
Examples here: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

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.

Sql Default Country, City, Town lists

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

mysql concat with trim

I've got a table which holds a bunch of addresses in cells labelled address | city.
I am attempting to merge the complete address into the common 'address, city' format.
Occasionally, in my database, I will have one of the location cells empty. Therefore, I do a IFNULL in my concat line, but I end up with a leading or trailing ','.
I have tried the 'trim' function along with my concat, but still get trailing ',' on occasion.
This is how I've written my query
SELECT TRIM(BOTH ',' FROM CONCAT(IFNULL(address,''), ', ', IFNULL(city,''))) FROM locals
Any idea why I would have this behavior? Is there a better way of building my concat statement?
I think your query is just missing a space after the comma in the BOTH statement. That seems to work for me
SELECT TRIM(BOTH ', ' FROM CONCAT(IFNULL(address,''), ', ', IFNULL(city,''))) FROM locals;
This is quite long but it seems to work.
SELECT TRIM(CONCAT(IFNULL(address,''), IF(address IS NOT NULL AND city IS NOT NULL, ', ',''), IFNULL(city,''))) FROM locals
So with this data:
address city
----------------------------
High Street Southampton
NULL London
Station Road NULL
London Road Brighton
You get this:
High Street, Southampton
London
Station Road
London Road, Brighton
stripping successive separators like "a,b,,,,c,d,e,,,,,eed, sffre" seems to be elusive of any elegant solution for GROUP_CONCAT
Methods used are to create views for each condition that avoids consecutive separators and then a union or join of the individual such views.