MySQL CAST and LIKE in WHERE clause - mysql

I want to write following query in MySQL and keep getting errors. I have no idea what's wrong, everything seems fine by me.
Query:
SELECT
*
FROM
client
WHERE
id IN (
SELECT
client_id
FROM
url
WHERE
',1,2,' LIKE '%,' + CAST(url_type_id AS CHAR(50)) + ',%'
)
It must have got something to do with the "'%,' + " part, but I honestly don't get it

You have to use CONCAT() to join 2 strings not "+", here is the query which you looking for
SELECT *
FROM client
WHERE id IN ( SELECT client_id
FROM url
WHERE ',1,2,' LIKE CONCAT('%,', CAST(url_type_id AS CHAR(50)), ',%')
)

Related

SQL SELECT * FROM tableA WHERE id IN (SELECT ids FROM tableB WHERE 1)

I'm looking for the result of this query:
SELECT * FROM procesos WHERE estatus=1 AND id_proceso IN (3,1,5,7,4,6,8)
But selecting the processes from the following query:
SELECT procesos FROM tipos_fichas WHERE id_tipo='1'
What I have tried without success is this:
SELECT * FROM procesos WHERE estatus=1 AND id_proceso IN (SELECT procesos FROM tipos_fichas WHERE id_tipo='1')
As you can see, it only brings me the first result... as if I could only read the first process before the comma (the 3 of: 3,1,5,7,4,6,8)
Does anyone have an idea how I can solve it?
MySQL converts the string value '3,1,5,7,4,6,8' to the integer value 3 discarding the rest of the string. That's why the rest of the values are not found. This is a silent feature of MySQL that I personally dislike.
You can use the LIKE operator to find the matches you want, as in:
select p.*
from procesos p
join tipos_fichas t on concat(',', t.procesos, ',')
like concat('%,', p.id_proceso, ',%')
where p.estatus = 1

Need regex expression in mysql select query

I am writing the mysql query where I need to compare data entered by user with the sku column in database tab
Query is like
SELECT *
FROM `is_product_info`
WHERE REPLACE( '-', '', sku ) LIKE '%ETI2006%'
so that if sku in database contains "-" in sku, I am replacing all hyphens with "" before comparing.
so whether sju no contains ETI2006 or ETI-2006, it will come in output
I think you may just like this
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
I didn't try on a running MySQL but this should work:
SELECT *
FROM `is_product_info`
WHERE sku REGEXP REPLACE('ETI-2006','-','-?');
I made the mistake in replace syntax, it works with the below one:
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
Using replace() on every row is what's slowing it down.
All of these approaches should be faster - see which one works best for you:
Option 1 - use LIKE twice:
WHERE sku LIKE '%ETI-2006%'
OR sku LIKE '%ETI2006%'
Option 2 - Use RLIKE once:
WHERE sku RLIKE 'ETI-?2006'

Wildcard + SQL Parameter in Procedure

I created a procedure in order to search for a product, but I got a problem, as I would like to use wildcards with a param (nam) , but I don't get them to work.
SELECT * FROM PRODUCTS WHERE NAME LIKE NAM + '%'
SELECT * FROM PRODUCTS WHERE NAME LIKE '%'+ NAM + '%'
...
Thank you so much.

Mysql sortation on multiple LIKE's

There are a lot of topics on sortation (like: Order Results By Occurrence) but these are all for one value.
I have a search field that people use with keywords; simply said the queries generated look like:
1 word:
SELECT *
FROM `meta`
WHERE (`keywords` LIKE '%bike%')
2 words:
SELECT *
FROM `meta`
WHERE (
`keywords` LIKE '%bike%'
OR `keywords` LIKE '%yellow%'
)
etc...
What I would like to do is sort the result on the most found keywords. How would I do this for an unknown amount of keywords LIKE's
Here is the general way to sort by the number of keyword matches in MySQL (using like):
SELECT *
FROM `meta`
ORDER BY ((`keywords` LIKE '%bike%') +
(`keywords` LIKE '%yellow%') +
. . .
) desc;
If you want to handle a flexible number of keywords, then you should use an appropriate relational data structure. Storing keywords in a single field (probably comma-separated) is not the best approach. You should have a separate table with one row per keyword.
EDIT:
To add in the number of keywords found, the expression can be put in the select statement:
SELECT m.*,
((`keywords` LIKE '%bike%') +
(`keywords` LIKE '%yellow%') +
. . .
) as NumKeywordsFound
FROM `meta` m
ORDER BY NumKeywordsFound desc;
You can also add a having clause to specify that at least one is found:
SELECT m.*,
((`keywords` LIKE '%bike%') +
(`keywords` LIKE '%yellow%') +
. . .
) as NumKeywordsFound
FROM `meta` m
HAVING NumKeywordsFound > 1
ORDER BY NumKeywordsFound desc;
If you want to find the number of times a keyword is found in each expression:
select m.*,
length(replace(keywords, 'bike', 'bike1')) - length(keywords) as NumBikes,
length(replace(keywords, 'yellow', 'yellow1')) - length(keywords) as NumYellows
FROM `meta` m

Joining two tables using the LIKE operator

I'm trying to join two tables based on two values being alike. I have this so far but I'm getting a SQL error as soon as I use the %.
SELECT downloads.d_key, payer_email
FROM paypal_log
INNER JOIN
downloads
ON downloads.d_key LIKE "%" + paypal_log.custom + "%"
The downloads.d_key will be within the paypal_log.custom.
Any help appreciated with this.
Try
SELECT 'one' + 'two' FROM DUAL
and see what you get. You'll want to use
LIKE concat('%', paypal_log.custom, '%')
MySQL concatenation operator is ||. You can also use the CONCAT() function:
SELECT downloads.d_key, payer_email
FROM paypal_log
INNER JOIN
downloads
ON downloads.d_key LIKE '%' || paypal_log.custom || '%' ;
As others pointed, you are probably doing something very, very wrong.