mysql concat with trim - mysql

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.

Related

How do I group together string rows with partial matching strings and then sum them together in SQL?

Artist
Points
Notorious BIG
34
Notorious BIG feat. blah blah
42
2pac
20
2pac feat. Dr. Dre
30
I would like to group and sum by Artist for a table to look like so:
Artist
Points
Notorious BIG
76
2pac
50
P.S. there are a lot of artists featuring other artists in this dataset so I cannot just do them all individually. Thanks for your help.
I was thinking maybe I should CREATE VIEW with all the feat.% removed then group the artists together? Not sure how I would go about doing that either.
CREATE VIEW no_feat AS
SELECT
REPLACE(artist, 'ft%', ' ')
FROM rankings;
I'm very new at this so I tried that and it obviously did not work. I dont think the replace function accepts wildcards
Considering the Artist starting with same string need to be grouped together, the below could work:
Select SUBSTRING(Artist, 0,CHARINDEX(' ', Artist, 0)), SUM(Points)
From Artist
Group by SUBSTRING(Artist, 0,CHARINDEX(' ', Artist,0))
You could use substring_index for this case.
This would get all the values before feat., if feat is missing entire string will returned.
SELECT SUBSTRING_INDEX(artist, 'feat.', 1) as Artist,
sum(points) as Points
FROM my_table
GROUP BY SUBSTRING_INDEX(artist, 'feat.', 1);
But, if the string contain spaces you should use TRIM as follows,
SELECT TRIM(SUBSTRING_INDEX(artist, 'feat.', 1)) as Artist,
sum(points) as Points
FROM my_table
GROUP BY TRIM(SUBSTRING_INDEX(artist, 'feat.', 1));
Result:
Artist Points
Notorious BIG 76
2pac 50
https://dbfiddle.uk/QpaO9bVZ

MySQL query: use each result/row to regex and count another table

Not sure if this is possible. With two tables, one is country codes:
e.g.
id | code | country
1 .us United States
2 .ru Russia
And so on (about 200+ rows)
The other is URLs:
http//:example.gov.us
http://example.gov.ru/index.php
http://xyz.gov.us/test.html
And so on.
I don't know what URLs will come in, so I would have to grab each country code and somehow query the URLs for any matches against the country codes and count how many there are for each.
e.g (?)
gov.[country code]
Ideally, I would like the output to be grouped by country name and counted, something like, using the above URLs as an example, it might result in:
country | total
United States | 2
Russia | 1
Like I said, not sure if this can be done in MySQL with regex, substrings etc. Would love to know if it can be.
You could use a query like this:
SELECT
c.country,
COUNT(*)
FROM
countries c INNER JOIN URLS u
ON SUBSTRING_INDEX(SUBSTRING_INDEX(url, 'http://', -1), '/', 1)
LIKE CONCAT('%', c.code)
GROUP BY
c.country
Please see fiddle here.
Using SUBSTRING_INDEX(url, 'http://', -1) you can get the whole string after the http://
http://example.gov.ru/index.php ---> example.gov.ru/index.php
then using SUBSTRING_INDEX(..., '/', 1) on this string you can get the part of the string before the first / or the whole string if there's no /
example.gov.ru/index.php ---> example.gov.ru
you can then check if example.gov.ru LIKE '%.ru'
select country, count(*) total
from country_codes c
join urls on urls.url RLIKE CONCAT("^http://[^/]+\\.gov\\.", c.code, "($|/)")
group by county

Stuck in getting distinct records when special characters are in the values

I have the following table data
London
Egypt
Newyork
Egypt*
India
China
London+
NewYork*+
When I use the following query select distinct CityName from cityTable, I am getting 8 records, But I am expecting to have only 5 records as London, Egypt, Newyork, India and china.
But in the above Egypt* and Newyork*+ should not be added to result set, but distinct is considered as a distinct records, can any one help me to get the required result.
I am using Mysql database
Use the MySQL group by statement
You can use this udf and replace non-alphanumeric characters.This will leave you just the citynames without special characters.Then you can get distinct above that.

Sql Query, Select distinct rows where not null

I have a question:
So I have the following data in a table"
id name city state phone price
234 Kevin Chicago IL 5555555555 550
234 Kevin Chicago IL 5555555555 NULL
234 Kevin Chicago IL 55555 NULL
234 Kevin Chicago IL NULL NULL
People have loaded data into a database and it is showing up like this because of character restrictions for each line...
So how can I tell my distinct query to get the most from each one..I just want this row returned:
234 Kevin Chicago IL 5555555555 550
And I cant do "where all rows are not null etc" because the last row can always be null too it depends.
Thanks a lot of your help!
Depending on what "the most" means exactly and depending on the data types of phone and price, this could be your query:
Edit: generalize query, get phone & price per person.
SELECT id, name, city, state, max(phone) AS phone, max(price) AS price
FROM tbl
GROUP BY id, name, city, state;
phone and price can come from different rows this way.
The manual on aggregate functions:
Unless otherwise stated, group functions ignore NULL values.
So, IFNULL or COALESCE are not required here.
try this
SELECT
ID,
MAX (IFNULL (NAME, ' ')) AS NAME,
MAX (IFNULL (CITY, ' ')) AS CITY,
MAX (IFNULL (STATE, ' ')) AS STATE,
MAX (IFNULL (PHONE, ' ')) AS PHONE,
MAX (IFNULL (PRICE, ' ')) AS PRICE
FROM MyTable
GROUP BY ID
The above gives you for each the "max" of each column - this can lead to content from different rows being combined...

Border-Line Cases: MySQL Point-in_Polygon Function Results in Duplicates When the Point is on the Border

I'm using MySQL to pull lat longs from a database and check whether or not they are in a particular neighborhood.
Everything works great, except if the point is on the border between two neighborhoods. Then, the point is included in both neighborhoods. It gets duplicated. What's the best way to handle points that are on borders?
Each point should be counted in only one neighborhood-- not both. Also, I don't want, for example, neighborhood A to get all of the border cases, but the bordering neighborhood, neighborhood B, to get zero cases.
Imagine that the point 30.3030, -70.7070 lies on the border between Newport and Oldport. Unfortunately, the point gets counted twice. It gets recorded as being in both Oldport and Newport.
type | latitude | longitude | neighborhood
small | 30.3030 | -70.7070 | Newport
small | 30.3030 | -70.7070 | Oldport
small | 30.3344 | -70.7274 | Anotherport
I use the select statement below:
SELECT t.type, t.latitude, t.longitude, s.neighborhoods
  FROM my_type_table t, neighborhood_shapes s
WHERE myWithin(POINTFROMTEXT( CONCAT( 'POINT(', t.latitude, ' ', t.longitude, ')' ) ) , s.neighborhood_polygons )) = 1
my_type_table has columns:
type (VARCHAR)
latitude (decimal)
longitude (decimal)
...and neighborhood_shapes has columns:
neighborhoods (VARCHAR)
neighborhood_polygons (geometry)
I use the myWithin function to test whether the point is in one neighborhood or another. Here's a link to it: myWithin function at mySQL forum . The function returns 1 when the point is in the polygon and 0 when it isn't in the polygon.
How would you solve this problem? Any advice?
Okay, I figured it out. There was a very small overlap in some of the polygons. So, when the function ran it put the point in both neighborhoods.
Thank you.
-Laxmidi