i have a mysql table with this sort of data
TACOMA, Washington, 98477
Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:
TACOMA, Washington
Is it possible though mysql or do i have to manually do it.
You can use :
SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
You can read more here.
And the update statement :
UPDATE my_table
SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
Where you need to replace my_table with your table name and my_col with the column you need to be updated.
Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-
SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
FROM SomeTable
substring_index(col, ',',-1)
will give the string from last index of comma to end of string
replace(col,concat(',',substring_index(col, ',',-1)),'')
Related
I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);
I have data stored in a comma-separated format and I would like to run a query so that the users are ordered by the second value in the column.
So for this sample data:
user | Data
________________________________________
player1 | 45471,2529,32196008193896,99
admin | 1136,2595,17760808279311,95
gamer | 13495,2432,32196008193896,98
________________________________________
The order would be (2595-2529-2432) which is admin => player1 => gamer.
As I mentioned in the comments, you should really try to avoid storing delimited lists like that.
However, you can write a query to parse out the second value by using the SUBSTRING_INDEX() function. This function takes a string, the delimiter character, and an integer whether or not to take from the left/right of the delimiter and for how many. In other words, to get everything left of the second comma, it would look like this:
SELECT SUBSTRING_INDEX(data, ',', 2)
FROM myTable;
Then, from that string, you want everything to the right of the first comma, so you'll have to nest the functions. (Note, this may hurt efficiency, but that's the downfall of delimited lists):
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(data, ',', 2), ',', -1)
FROM myTable;
Then, you can just order by that value:
SELECT *
FROM myTable
ORDER BY SUBSTRING_INDEX(SUBSTRING_INDEX(data, ',', 2), ',', -1) DESC;
Here is an SQL Fiddle example.
How can I get the number of values specified in a MySQL SET column?
Is there any built function in mysql ?
SELECT id, LENGTH(colname) - LENGTH(REPLACE(colname, ',', '')) + 1 AS set_count
FROM YourTable
This answer:
https://stackoverflow.com/a/10738323/1176436
Assuming you don't have values like '123,123,' (note the comma at the end) this should work:
SELECT
LENGTH(yourColumn) - LENGTH(REPLACE(yourColumn, ',', '')) + 1 AS numberOfItemsInRow
FROM yourTable;
Find more information here.
But it would really be better to normalize your database!
Is there a way to get a value like this one: "300, 400, 500, 300" check each number separated with comma and if it is doubled delete it. So the value will look like this : "300, 400, 500".
I could do it in PHP script but I just wonder if it is possible using MySQL.
Create a temp table with unique index, insert values ignoring duplicate errors, select all records from the temp table, delete the table.
Quick play, but to get the unique values for each row you could use something like this
SELECT Id, GROUP_CONCAT(DISTINCT aWord ORDER BY aWord ASC)
FROM (SomeTable.Id, SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(concat(SomeColumn, ','), ' ', aCnt), ',', -1) AS aWord
FROM SomeTable
CROSS JOIN (
SELECT a.i+b.i*10+c.i*100 + 1 AS aCnt
FROM integers a, integers b, integers c) Sub1
WHERE (LENGTH(SomeColumn) + 1 - LENGTH(REPLACE(SomeColumn, ',', ''))) >= aCnt) Sub2
GROUP BY ID
This relies on having a table called integers with a single column called i with 10 rows with the values 0 to 9. It copes with up to ~1000 words but can easily be altered to cope with more
Probably easiest to use an INSERT / ON DUPLICATE KEY UPDATE to use this to make the values unique.
Let's say I have the following rows:
id data (TEXT)
1 abc"100"dfg
2 abc"200"dfg
3 abc"150"dfg
Would it be possible to order the results by the number in quotes (abc"X"dfg) and return the number?
If the numeric fields are always in the same place you could write
ORDER BY 0 + SUBSTRING(data, 5, 3)
or if they are always bound by the only double quotes in the string you could write
ORDER BY 0 + SUBSTRING_INDEX(SUBSTRING_INDEX(data, '"', -2), '"', 1)
Although the substrings are numeric they are still string values and MySQL will sort them lexically. For a numeric sort they must be forced to numeric values by adding zero.
SELECT *,SUBSTRING(`data`,4,3) AS num FROM table ORDER BY num ASC