MySQL JOIN two tables using LIKE and substrings - mysql

I have been trying to join two MYSQL tables with columns that contain a common substring.
My first table trim_table is a data set of substrings and its id. Each row contains an id, and a substring such as "1717 WINTON DR".
The second table wp_prop contains a columnfield_313 that may contain the substring found on the first table. An example value of field_313 would be "THE SARGENT III # 1717 WINTON DR"
The following is the query that I formulated to join both tables:
SELECT *, wp_prop.id as prop_id, trim_table.id as post_id, wp_prop.field_313
as prop_name, trim_table.trim_value as post_name
FROM
(SELECT id, TRIM(SUBSTRING(post_title, LOCATE('#', post_title)+1)) as trim_value
FROM wp_nd333j_posts WHERE post_type="fplan") as trim_table
JOIN
wp_nd333j_wpl_properties as wp_prop
ON trim_table.trim_value LIKE CONCAT('%', wp_prop.field_313, '%')
Unfortunately, the query does not return any rows. I have decomposed the query into different parts to verify that they work.
It is certain that the substring dataset returns the correct rows by executing the following query:
SELECT * FROM
(SELECT id, TRIM(SUBSTRING(post_title, LOCATE('#', post_title)+1)) as trim_value FROM wp_nd333j_posts WHERE post_type="fplan") as trim_table
I can also verify that my wp_prop table contains a substring similar to the example I posted above by executing the following query:
SELECT * FROM wp_nd333j_wpl_properties as wp_prop WHERE field_313 LIKE "%1717 WINTON DR%"
I also verified that my substring dataset contains my desired substring by executing the query:
SELECT * FROM
(SELECT id, TRIM(SUBSTRING(post_title, LOCATE('#', post_title)+1)) as trim_value FROM wp_nd333j_posts WHERE post_type="fplan") as trim_table
WHERE trim_value = "1717 WINTON DR"

You have just switch your fields in ON clause:
ON wp_prop.field_313 LIKE CONCAT('%', trim_table.trim_value, '%')
http://sqlfiddle.com/#!9/616a0/1
SELECT
trim_table.id as post_id,
trim_table.trim_value as post_name,
wp_prop.id as prop_id,
wp_prop.field_313 as prop_name
FROM (
SELECT id,
TRIM(SUBSTRING(post_title, LOCATE('#', post_title)+1)) as trim_value
FROM wp_nd333j_posts
WHERE post_type="fplan"
) as trim_table
INNER JOIN
wp_nd333j_wpl_properties as wp_prop
ON wp_prop.field_313 LIKE CONCAT('%', trim_table.trim_value, '%')

Related

sql request too long ... How to simplify?

i would like to reduce the process time of my SQL request (actually it runs 10 minutes ...)
I think the problem come from the nested SQL queries.
(sorry for my english, i'm french student)
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount`
FROM globe_statistique
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21`
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers`
WHERE `gst.page` = 'send_order'
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d')
UNION
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, '-'
FROM globe_statistique
WHERE `gst.page` NOT LIKE 'send_order' "
AND (`gst.codeAP21`,`gst.date`) NOT IN
( SELECT `gst.codeAP21`,`gst.date` FROM globe_statistique
WHERE `gst.page`='send_order');
Thanks
try this:
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount`
FROM globe_statistique
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21`
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers`
WHERE `gst.page` = 'send_order'
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d')
UNION
SELECT DISTINCT t1.`gst.codeAP21`, t1.`gst.email`, t1.`gst.date`, '-'
FROM globe_statistique t1
left join globe_statistique t2 on t1.gst.page =t2.gst.page and t1.gst.date =t2.gst.date and t2.gst.page =send_order
WHERE `gst.page` <> 'send_order' AND t2.gst.date is null
But i recomment to rename your column names and remove the dots.
Also use EXPLAIN to find out why the query is slow and add the correct index
try to avoid the use of distinct. To this end, UNION ALL should be used. Group by at the end gives the same result:
select codeAP21, email, date, amount
from ( --> your query without distinct but with UNION ALL <-- )
group by codeAP21, email, date, amount
see: Huge performance difference when using group by vs distinct

Working LIKE statement with % on column names but rows repeated

I have my SELECT query used with LIKE statement working but am shocked; that my rows fetched are repeated, and i don't know why?
SELECT *
FROM questions, counts
WHERE counts.test_coursecode LIKE '%' || questions.coursecode || '%'
You must include the inner join of the two tables
SELECT *
FROM questions q inner join counts c on a q.id and c.fk
WHERE counts.test_coursecode LIKE CONCAT('%', questions.coursecode, '%')
or
SELECT *
FROM questions q , counts c
where a q.id and c.fk
and counts.test_coursecode LIKE CONCAT('%', questions.coursecode, '%')
I'm assuming you want to use
SELECT *
FROM questions, counts
WHERE counts.test_coursecode LIKE CONCAT('%', questions.coursecode, '%')
instead.
|| is not the concatenation operator but the logical OR in the sql dialect of MySQL.
Your query will match every row, because any value will match the first expression, it evaluates to
SELECT *
FROM questions, counts
WHERE counts.test_coursecode LIKE '%' -- that's true, if test_course_code is not null
OR questions.coursecode
OR '%'

MySQL update query on a single table with value from joined table

I have two tables named :
url_alias
product
url_alias TABLE has following fields : id, query, keyword
id is numeric,
query is of the form "product_id=45"
keyword is of the form "product-actual-name-in-url-friendly-manner"
product table has following fields: product_id, name, language_id
What I want is to update url_alias table "keyword" field with proper url friendly string which I have generated by using mysql REPLACE function and is aliased as NEW_KEYWORD but the url friendly string needs to be auto-generated from a join of both tables .
Following query shows a SELECT query on tables properly:
SELECT u.url_alias_id, u.query, u.keyword, p.name, REPLACE( p.name, ' ', '-' ) AS NEW_KEYWORD
FROM url_alias u, product p
WHERE u.query = CONCAT( "product_id=", p.product_id )
AND p.language_id =3
Please help me in an update query by using this query
Try it:
UPDATE url_alias u
JOIN product p
ON u.query = CONCAT( "product_id=", p.product_id )
SET u.keyword=REPLACE( p.name, ' ', '-' )
WHERE p.language_id =3;

mysql join query with encrypted columns not decrypting or grouping

I have a query on a table with some encrypted fields like so:
SELECT groups.`group_id` AS id, `description_s`,
AES_DECRYPT(`description_l`, 'decryption_key') AS `description`,
AES_DECRYPT(groups.`email`, 'decryption_key') AS email,
...
FROM groups
WHERE ...
GROUP BY `id`, `description_s`,`description`,`email`,...
ORDER BY id DESC
This works as expected, giving me the decrypted output. However if I add a join to another table.
SELECT groups.`group_id` AS id, `description_s`,
AES_DECRYPT(`description_l`, 'decryption_key') AS `description`,
...
FROM groups
INNER JOIN details on details.`group_id` = groups.`group_id`
WHERE ...
GROUP BY `id`, `description_s`,`description`,`email`,`customer_name`
ORDER BY id DESC
The result columns are no longer decrypted and the grouping only works if I put the AES_DECRYPT(description_l,dec_key) rather than the alias description in the 'group by' clause. Still leaving me with encrypted columns.
I found a workaround by using subqueries, and doing the decryption on the outside.
SELECT id,description_s, AES_DECRYPT(`description_l`, 'decryption_key') as description
FROM (
SELECT groups.`group_id` as id, `description_s`, `description_l`
FROM groups
INNER JOIN details ON details.`group_id`=groups.`group_id`
WHERE ...
GROUP BY `id`, `description_s`,description_l
) subq
ORDER BY id DESC
I am curious to know what exactly is going on. Why does the column not get decrypted at all in the second example above? Is there a way to get the grouped,decrypted result without using subqueries?

SQL: Display multiple record values in one field from a subquery

I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess