I want to use separator for group_concat() function in mysql.But I want to change the separator dynamically.I mean the separator value is coming from a table and that is different for every row.
I couldn't found any solution for that please help me.
Finally got a solution.
Let's say we have some MySQL routine. We need to GROUP_CONCAT some value with special SEPARATOR (default is ','). But SEPARATOR isn't static, it's got from another table, for ex. from some "settings" table.
DECLARE url_delimiter VARCHAR(255);
SELECT catalog_url_delimiter
INTO url_delimiter
FROM settings;
We can't use variable as SEPARATOR parameter directly:
-- doesn't work
SELECT GROUP_CONCAT(`some_table`.id SEPARATOR url_delimiter)
FROM <some query>
But we can use some dummy separator and replace it with valid as below:
SELECT
REPLACE(
GROUP_CONCAT(`some_table`.id SEPARATOR 'some-tricky-dummy-separator'),
'some-tricky-dummy-separator',
url_delimiter
)
FROM <some query>
I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.
I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
I need to remove the last part of a string in a column where I have a field named "path" that looks like:
images/prop/images/2034/22399_2034.JPG
I need everything after the last "/" to be deleted, in order to have
images/prop/images/2034/
instead of
images/prop/images/2034/22399_2034.JPG
I have no idea if this is possible. Thanks.
You can combine MySQL's TRIM() and SUBSTRING_INDEX() functions:
SELECT TRIM(TRAILING SUBSTRING_INDEX(path, '/', -1) FROM path)
FROM my_table
See it on sqlfiddle.
I would like to sort a result from a MySQL query by the number of words in a specified column.
Something like this:
SELECT bar FROM foo ORDER BY WordCountFunction(bar)
Is it possible?
As far as I know, there is no word count function in MySQL, but you can count the number of spaces and add one if your data is formatted properly (space separator for words, no spaces at beginning/end of entry).
Here is the query listing longest words first:
SELECT bar FROM foo ORDER BY (LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) DESC
Using this method to count the number of words in a column, your query would look like this:
SELECT bar FROM foo ORDER BY (LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) DESC
Yes, sort of. It won't be 100% accurate though:
SELECT SUM(LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) as count
FROM table
ORDER BY count DESC
But this assumes the words are separated by a space ' ' and doesn't account for punctuation. You can always replace it with another char and it doesn't account for double spaces or other chars either.
For complete accuracy, you could always pull the result out and word-count in your language of choice - where accurate word-count function do exist!
Hope this helps.