I'm trying to select all values in a table based off the userinput. My issue is when I try to write the query and include the '%' character, I always run into errors. I've tried at least 10 different placements, but I always receive an error. The database being used is MySQL.
SELECT * FROM clients WHERE UserName = %:UserName;
If you want to use the operator LIKE with the wildcard '%' you must concatenate it to the user's input:
SELECT *
FROM clients
WHERE UserName LIKE CONCAT('%', :UserName);
If you want all rows with UserName that contain :UserName:
WHERE UserName LIKE CONCAT('%', :UserName, '%');
Related
I have a field called EMAIL_ADDRESS. One of the records would be:
john#gmail.com, mike#gmail.com, joe#yahoo.com, george#yahoo.com, fred#gmail.com
I wan to remove all yahoo addresses in my SELECT query to get:
john#gmail.com, mike#gmail.com, fred#gmail.com
If I use
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, 'joe#yahoo.com,', '')
this works.
If I want to remove ALL yahoo email addresses this doesn't work:
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, '%#yahoo.com,', '')
because wildcards don't seem to work as it's looking for % in the string.
You should probably fix your table design and stop storing CSV lists of email addresses. Instead, get each email onto a separate record. As a short term fix, if you're running MySQL 8+, you may use REGEXP_REPLACE():
UPDATE yourTable
SET EMAIL_ADDRESS = REGEXP_REPLACE(
REGEXP_REPLACE(EMAIL_ADDRESS, '(, )?\\S+#yahoo\\.com,?', ','), '^,+|,+$', '')
WHERE EMAIL_ADDRESS LIKE '%#yahoo.com%';
If you don't need to udpate records but you want them only in the SELECT query you can use NOT LIKE operator
SELECT * FROM your_table WHERE email NOT LIKE '%yahoo.com'
So you get records that doesn’t match the like pattern
i want some correction here. i want to select all people with name fred in database
Here's my query:
SELECT * FROM tdble WHERE CONCAT(name) LIKE CONCAT('%', REPLACE('fred', '')'%')
What you are asking can be simply achieved by either using the "=" operator of the wildcard operator "like" statement.
If you wish to find all records that have an exact match to the name 'Fred' then you should model your query as so:
Select * From tdble Where Name = 'fred'
However, if you want to get all results where the names have 'fred' included in it somewhere use the wildcard operator.
Select * From tdble Where Name like '%fred%'
Also you can further model your query to know where exactly in which form you want 'fred' to appear. Example if you want 'Fred' to be as the last characters of your name string, for instance you wish to get names which ends with fred then model your query like this:
Select * From tdble Where Name like '%fred'
(you will get results like 'alfred', provided there is an alfred in your table)
However if you wish to get all names that begin with fred, model the query like this:
Select * From tdble Where Name like 'fred%'
(you will get results like 'fredinane', provided there is a fredinane in your table)
Cheers
If you want to fetch record with name 'fred', you can simply do Select * from TableName Where Name = 'fred'.
If you want to fetch records which their names' string contain 'fred', you have to use select * from TableName where Name like '%fred%'
I observed that this query returns all values from the database.
SELECT * FROM projround3.add_user where user_email like '%' '%';
Why is that?
This query:
SELECT *
FROM projround3.add_user
WHERE user_email like '%' '%';
Would not be valid in most databases. Some interfaces concatenate adjacent strings, so this is interpreted as:
SELECT *
FROM projround3.add_user
WHERE user_email like '%%';
The two wildcards are the same as one. This will match every non-NULL value.
If you want to find emails with a space, then correct logic is:
SELECT *
FROM projround3.add_user
WHERE user_email like '% %';
So I have a list of MerchantNames like so, stored in the Merchant table:
MerchantName
------------
Al's
Bart's
Mo's Cafe
And I want to do a query like so:
select merchantDetail
from merchant
where merchantDetail like '%' + merchantName + '%'
I already tried the above but (as i suspected) it complained about an error in MySQL syntax. I want to do this for every merchant Name in the column. Is something like this possible? Thank you very much.
The correct syntax for the WHERE clause in MySQL would be:
where merchantDetail like concat('%', merchantName, '%')
The + is SQL Server syntax for string concatenation. Note that because you are using LIKE, the '%' and '_' characters have special meanings. This could affect any merchant names with those characters.
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.