mysql regexp two words enclosed with any special characters - mysql

+-----+------+
| A | B |
+-----+------+
| gan | esh |
| dhi | nesh |
+-----+------+
I have a table like this. I want to check this with another table has column
+----------------+
| C |
+----------------+
| !!dhin!!esh |
| gan!!esh.. |
| $$$gan%%%esh.. |
+----------------+
The following query works fine
select * from table1 a, table2 b where c like concat('%',a,'%',b'%')
but what I want is starting, ending, middle should contain only non-alphanumeric. I am a newbie to regexp.

CONCAT("[^[:alnum:]]", a, "[^[:alnum:]]+", b, "[^[:alnum:]]")
Or perhaps
CONCAT("\\b", a, "[^[:alnum:]]+", b, "\\b")

Related

INNER JOIN same value, but the difference is the other table are having extra word in front of the value

As I said in the title, or maybe my question is a little bit confusing. Here it is....
So, I want to combine 2 tables using INNER JOIN (ofcourse) with some difference.
This is my tables
Table 1, PK = steam_id
SELECT * FROM nmrihstats ORDER BY points DESC LIMIT 4;
+---------------------+----------------+--------+-------+--------+
| steam_id | name | points | kills | deaths |
+---------------------+----------------+--------+-------+--------+
| STEAM_0:1:88467338 | Alan14 | 50974 | 5438 | 12 |
| STEAM_0:0:95189481 | ? BlacKEaTeR ? | 35085 | 24047 | 316 |
| STEAM_0:1:79891668 | Lowell | 34410 | 44076 | 993 |
| STEAM_0:1:170948255 | Rain | 29780 | 30167 | 278 |
+---------------------+----------------+--------+-------+--------+
4 rows in set (0.01 sec)
Table 2, PK = authid
SELECT * FROM store_players ORDER BY credits DESC LIMIT 4;
+-----+-------------+---------------+---------+--------------+-------------------+
| id | authid | name | credits | date_of_join | date_of_last_join |
+-----+-------------+---------------+---------+--------------+-------------------+
| 309 | 1:88467338 | Alan14 | 15543 | 1475580801 | 1482260232 |
| 368 | 1:79891668 | Lowell | 10855 | 1475603908 | 1482253619 |
| 256 | 1:128211488 | Fuck[U]seLF | 10422 | 1475570061 | 1482316480 |
| 428 | 1:74910707 | Mightybastard | 7137 | 1475672897 | 1482209608 |
+-----+-------------+---------------+---------+--------------+-------------------+
4 rows in set (0.00 sec)
Now, how can I use INNER JOIN without doing like removing "STEAM_0:" or adding it. Also with explanation, please
You can join witn like operator, e.g.:
SELECT n.*, sp.*
FROM nmrihstats n JOIN store_players sp ON n.steam_id LIKE CONCAT('%', sp.authid);
Here's the SQL Fiddle.
Another approach would be to use String functions of MySQL to extract out relevant part from steam_id but I believe that's not what you want:
SELECT SUBSTR(steam_id, LOCATE('STEAM_0:', steam_id) + CHAR_LENGTH('STEAM_0:'))
FROM nmrihstats;
it is not possible, you need to remove "STEAM_0:", matching with WHERE, using substring for remove STEAM_0: from column equals to column in other table, or a new field into the T1 without "STEAM_0:", that 2 columns match for INNER JOIN

MySQL Substring between two DIFFERENT strings where the second needle comes AFTER the first

I have to extract certain data from a MySQL column. The table looks like so:
+----+---------------------+------------------------+
| id | time | data |
+----+---------------------+------------------------+
| 1 | 2016-10-28 00:12:01 | a Q1!! AF3 !! ext!! z |
| 2 | 2016-10-28 02:19:02 | z !!3F2 !AF66-2!! !!a |
| 3 | 2016-10-28 11:35:03 | AF!a !!! pl6 f !!! dd |
+----+---------------------+------------------------+
I want to grab the string from column data between the characters AF and the NEXT occurrence of !! So ideally the query SELECTid,[something] AS x FROM tbl would result in:
+----+------+
| id | x |
+----+------+
| 1 | 3 |
| 2 | 66-2 |
| 3 | !a |
+----+------+
Thoughts on how to do this? All the other questions I see don't quite relate, as they don't deal with finding the first occurrence of the second needle (!!) AFTER the first needle (AF).
There may be faster ways to do this but this is a good start:
select substring_index(substring_index(data, 'AF', -1), '!!', 1)

Sphinxsearch json values comparison

I''m working with JSON functionality in sphinx 2.2.2.INDEX table looks like:
mysql> SELECT subsite_min_cnt, version_content_cnt FROM mobile_collection;
+------------------------+-----------------------------------------------------------+
| subsite_min_cnt | version_content_cnt |
+------------------------+-----------------------------------------------------------+
| {"85":3,"75":4,"65":5} | {"10003":4,"10008":5,"10009":5,"11000":7,"1":1,"10000":3} |
| {"85":6,"75":4,"65":5} | {"46":1,"201":1,"11000":1,"10010":1} |
+------------------------+-----------------------------------------------------------+
And I work with two values from JSONs:
mysql> SELECT subsite_min_cnt.85 as a, version_content_cnt.10008 as b
FROM mobile_collection;
+------+------+
| a | b |
+------+------+
| 3 | 5 |
| 6 | NULL |
+------+------+
I try to compare these two value and here is what i get (the json_autoconv_numbers is equal to 1):
mysql> SELECT subsite_min_cnt.85 as a, version_content_cnt.10008 as b
FROM mobile_collection WHERE b IS NOT NULL and a < b;
ERROR 1064 (42000): sphinxql: syntax error, unexpected IDENT, expecting CONST_INT (or 3 other tokens) near 'b'
Or:
mysql> SELECT subsite_min_cnt.85 < version_content_cnt.10008 as b
FROM mobile_collection;
+------+
| b |
+------+
| 0 |
| 0 |
+------+
So, the question is: does comparison of two json values work in sphinxql? Or, maybe, I compare items in wrong way...
Despite json_autoconv_numbers = 1, the type conversion solved my problem:
mysql> SELECT integer(subsite_min_cnt.85) < integer(version_content_cnt.10008) as c, subsite_min_cnt.85, version_content_cnt.10008
FROM mobile_collection where c > 0;
+------+--------------------+---------------------------+
| c | subsite_min_cnt.85 | version_content_cnt.10008 |
+------+--------------------+---------------------------+
| 1 | 3 | 5 |
+------+--------------------+---------------------------+

"SELECT" selects wrong rows when umlauts are present in table

The structure of table 'textconstraint' (collation utf8_general_ci) is as follows:
+-----+---------+
| id | pattern |
+-----+---------+
| 11 | Ä |
| 27 | A |
+-----+---------+
When I query
SELECT * FROM textconstraint WHERE pattern = 'A' LIMIT 1;
The following rows are selected
+----+---------+
| id | pattern |
+----+---------+
| 11 | Ä |
+----+---------+
Why A-umlaut is selected instead of A?
P.S. I do SET NAMES UTF8
You can try this:
SELECT *
FROM textconstraint
WHERE pattern = BINARY 'A'
See this fiddle.

Transforming one record into multiple records of a resultset

I'm creating a MySQL view for a server application that connects directly to the database server. I didn't like the way the application's schema is so I went with my own structure. Originally, I was going to use the data to create static configuration files, but that is no longer an option.
Anyway, I'll be using several rows from different tables to create this view. There is a 1:many relationship between table A and the view and a 1:1 relationship for table B. It's the 1:many that's throwing me off.
In table A, I have the following:
+--------------------------------+
| id | name | timeout | any |
+--------------------------------+
| 1 | Main | 10 | 1 |
+--------------------------------+
In table B, I have the following:
+-------------------+
| id | a_id | route |
+-------------------+
| 1 | 1 | 123 |
+-------------------+
| 2 | 1 | 321 |
+-------------------+
For the view, the two tables will be joined like so:
+-------------------------------------+
| name | app | data |
+-------------------------------------+
| Main | timeout | 10 |
+-------------------------------------+
| Main | dialany | 1 |
+-------------------------------------+
| Main | routeto | 321 |
+-------------------------------------+
| Main | routeto | 123 |
+-------------------------------------+
I'm not even sure if there's a name for this (or if it's even possible) but any help to get started would be fantastic.
You'd use a UNION to get the individual rows from the first table, something like this:
SELECT name, 'timeout' as app, timeout FROM A WHERE id = 1
UNION SELECT name, 'dialany' as app, `any` FROM A WHERE id = 1
And then UNION in the other table:
UNION SELECT A.name, 'routeto' as app, B.data FROM A
JOIN B ON A.id = B.a_id
WHERE A.id = 1
And you should have what you want.