MySQL JOIN ON a subquery? - mysql

Table a looks like this:
uuid
----------------
0681a8ff0e114680b2688c538d92f3cb
148ba55922544c1b8ea8e7d43ffb8095
Table b looks like this:
uuid | username
-----------------------------------------------
0681a8ff-0e11-4680-b268-8c538d92f3cb | test123
148ba559-2254-4c1b-8ea8-e7d43ffb8095 | poop123
ac123b2a-6546-8979-3213-cb426aac426b | blabla
How do I select all values from table a with their respective username? Note that table a has the UUIDs without the hyphens, while table b has them with hyphens. (Both are VARCHARS though)
I know how to select the UUIDs from table a with the dashes added:
SELECT CONCAT_WS('-',MID(uuid,1,8),MID(uuid,9,4),MID(uuid,13,4),MID(uuid,17,4),MID(uuid,21,1000))
I also know how to join two tables based on a column, but I can't figure out how to add the dashes and do the join all in one query.

Just remove the hyphens in the on clause:
select . . .
from a join
b
on a.uuid = replace(b.uuid, '-', '');
Then, go back and figure out how to fix the original data. Your keys should have the same type and format for foreign key references.

Related

Mysql Insert Select Common Phrase into new table

I have a Table which with many rows and looks like something below.
id Name
1481 Carrier_186_CLI
1469 Carrier_186_OUT
1480 Carrier_107_CLI
1483 Carrier_107_OUT
All i want is to copy the rows into another table which has the common phrase , like
id Name
1 Carrier_186
2 Carrier_107
I tried this
Insert into newtable(Name)select Name from oldtable group by Name
I know it wont work but i am unable to find a solution for this. I am new to mysql.
You can use string function substring_index to pick value before 2nd underscore
Insert into newtable(Name)
select substring_index(Name,'_',2)
from oldtable
group by substring_index(Name,'_',2)
Demo

MySQL using like to match specific string

In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !
if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link
The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.

MYSQL 5.6 : Select where value is in CSV colum

I have a database (Mysql 5.6) like this :
id | value
--------------------------
1 | "value1;value2;value3"
2 | "value4;value5;value6"
I'd like to make a request like :
SELECT id FROM table WHERE 'value5' IS IN value
return --> 2
SQLFiddle : http://sqlfiddle.com/#!9/b5c347
I cannot change the database schema
Use LIKE syntax, but use it like this:
SELECT id
FROM table
WHERE
value LIKE 'value5;%' OR
value LIKE '%;value5' OR
value LIKE '%;value5;%' OR
value LIKE 'value5';
This is a dirty solution, and not possible to index, but should fit your needs (On a smallish table in a reasonable amount of time)
Another solution (If you're sure your values don't contain commas):
SELECT id
FROM table
WHERE
FIND_IN_SET('value5', REPLACE(value, ';', ',')
If all the values are different, you might be able to use LIKE:
SELECT id FROM table WHERE value LIKE '%value5%';
Just keep in mind that it won't work in case you have some value that contains the other, like
value2;value3;value56
Or use a RegExp, like this:
SELECT id FROM table WHERE value REGEXP '(^|[^a-z0-9])value5([^a-z0-9]|$)'

select a column value that matches with a value of another column

I have a MySQL table called 'Employee'. It has seven columns, but only two column values are related to my question. The two column names are FullName and Name.
Here are some sample values of the two columns in the table for better understanding.
FullName Name
----------
MichealPhilips | Philips
Louisfarak | louis
Waynebruce | kirten
I want to find the rows where FullName value contains the value of name. So in my example the answer should be MichealPhilips and Louisfarak but not Wayne bruce because FullName(Waynebruce) does not contain Name(Kirten).
I have tried a query some thing like this:
SELECT * FROM Employee WHERE FullName LIKE '%' || Name || '%';
But it seems like a wrong query. It is printing all the rows in the table and I don't know why.
Can someone please help me in this? Are there any other ways to write this query? Is my query wrong?
This should do it for you...
SELECT
a.FullName
FROM
Employee a
WHERE
a.FullName LIKE CONCAT('%', a.Name, '%')

MySQL Search / compare Keywords in table 1 and Table 2 :

Hey , i got this challenge , i got a MySQL DB table 1 with queries or text and table 2 with synonyms and misspellings as CSV [comma separated values]. Now i want to test if any query word in table 1 matches a synonym or misspelling in table 2 , then i would select them separately .
example :
table 1 row: "i am sick of HIV AIDS , what can i do?"
table 2 : HIV,AIDS,Cancer,TB,Chicken Pox ......
so this would be selected because at least there is a MATCH word in table 1 that matches a synonym in table 2.
On a MyISAM table:
SELECT *
FROM table1 com, table2 syn
WHERE MATCH (com.body) AGAINST(syn.list IN BOOLEAN MODE);
This will work even if your don't have a FULLTEXT index on com.body, but with a FULLTEXT index this will be super fast.
If you wrap your synonym lists into double quotes, like this:
"HIV", "AIDS", "chicken pox", "swine flu"
, only the whole phrases will be matched, not just split words.
select strings.text
from table1 strings
where exists (
select 1
from table2 sm
where instr(strings.text, sm.word) <> 0
)