Setting boundary within MYSQ Query LIKE - mysql

I need to search products that are B196 not B196Y. Products are saved within Database in following format
B196 - Hair Band - Pink
SELECT * FROM
sn_unit
where
product LIKE 'B196%Hair Band%Pink';

You could just add space:
SELECT *
FROM sn_unit
where product LIKE 'B196 %Hair Band%Pink';

LIKE can be used to filter out results as well; something like this would be more precise.
SELECT *
FROM sn_unit
WHERE product LIKE 'B196%Hair Band%Pink'
AND product NOT LIKE 'B196Y%'
;

As you are looking your product store with string B196 so, you can search for that string LIKE %B196%. That will find out B196 and you are not looking for B196Y that need to exclude from your query. Thats why exlude it using NOT LIKE %B196y%. This query using AND operator that will return true when string contain only B196.
SELECT *
FROM sn_unit
WHERE product LIKE '%B196%'
AND product NOT LIKE '%B196Y%' ;

Related

transfer several parameters from a form into SQL query

i am working on a table that includes a filter function.
For the filter i use a form where i enter the parameters.
Those are added to a string which is my SQL query.
So far it works fine.
There is oine input field where multiple parameters canbe added.
The plan is to seperate them with ; .
For example 520;521;522
My plan was to use str_replace to convert this in to sql Code.
For example
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
But some how this code does not show the expected results.
I only get results for '%520%'
How do i need to adjust this query in order to have the sql query working?
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
In another input field i search for names.
Here the query looks like this...
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%')
This works fine!
The multiple like should be written as,
SELECT *
FROM MaschinenVorgangslisteMitHV
WHERE VorgangNr REGEXP '520|522|523';
I believe you need to add VorgangNr LIKE after every OR.

how to show a specific word in a column?

I have a table which has various columns. One of them is called APP_DATA, and its fields are like this:
s:12:"project_code";s:4:"1025";s:18:"project_code_label";s:4:"1025"
I want to have a result that the output shows just project_code in the fields( not the other things)
I wrote a query like this:
SELECT A.APP_DATA AS app_data,ACV.APP_NUMBER AS app_number,
ACV.APP_STATUS AS app_status
FROM APP_CACHE_VIEW AS ACV
LEFT JOIN APPLICATION AS A ON
ACV.APP_NUMBER = A.APP_NUMBER
WHERE A.APP_DATA LIKE '%project_code%'
Unfortunately, the output of my query shows all of contents of APP_DATA. I like to have just a specific word!(just project_code)
How can I solve it?
Looking forward to your response
One easy way is to use SUBSTRING.
Example:
SELECT SUBSTRING(A.APP_DATA, 7, 12) AS app_data,ACV.APP_NUMBER AS app_number,
ACV.APP_STATUS AS app_status
FROM APP_CACHE_VIEW AS ACV
LEFT JOIN APPLICATION AS A ON
ACV.APP_NUMBER = A.APP_NUMBER
WHERE A.APP_DATA LIKE '%project_code%'
Example link
Careful, the index starts from 1.
For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

How can I use OR operator with 2 tables?

I'm trying to making a search option for my website project. I have to search 2 columns from 2 tables. After that, I'll write that query in my php code. Then it will list all the data about it. But it seems like I'm doing it wrong. What should I do?
select *
from mudurler,subeler,veriler
where mudurler.sube_id=subeler.sube_id
and veriler.sube_id=subeler.sube_id
and subeler.sube_ad like "%this%" or mudurler.adSoyad like "%that%"
When I go, if there is a valid value on sube_ad it works perfectly. But when i try to put valid value on adSoyad MySQL turns an empty result no matter what the value is.
You would have no problem if you used proper, explicit, standard JOIN syntax:
select *
from mudurler m join
subeler s
on m.sube_id = s.sube_id join
veriler v
on v.sube_id = s.sube_id
where s.sube_ad like '%this%' or
m.adSoyad like '%that%';
May be you should try:
and (subeler.sube_ad like "%this%" or mudurler.adSoyad like "%that%")

$sql="SELECT * FROM glasanje where ime=''

I need help . Here's the code . I want to vote from the table to pull out all the names , and to introduce them across the chart .
I tried to use * but it does not work :)
$sql="SELECT * FROM glasanje where ime='*'
if you want to extract only the names (ime), you have to put it in the SELECT statement, try this way:
SELECT ime
FROM glasanje
SELECT * means that you want to see all the columns, SELECT ime just the column ime.
The WHERE clause is used to select a subset of rows, for example if you want just the people with name Filip, you can write WHERE ime ='Filip'
Anyway these are a basic SQL functionality, I think you should try to study it a bit.

mysql: selecting rows if a number is in a list field (ie. userIDs = "1,2,3", select * from table where 1 in userIDs)

i store lists of ids inside fields (by list i mean val1,val2,val3,...), and i want to select rows from a table where the field has valX in the list.
i thought this would work, but apparently not :(
SELECT *
FROM userbulletins
WHERE 25 IN `currentMessages`
OR 25 IN `removedMessages`
OR 25 IN `readMessages`
currentMessages, removedMessages, and readMessages are fields with ids stored as lists
i've also tried
SELECT *
FROM userbulletins
WHERE "25" IN(`currentMessages`)
both return nothing (and should return 1 row in my test db)
any idea what i'm doing wrong? or if this is even possible?
thanks!
If I understand correctly, you have a denormalized table where the currentMessages value can be "val1,val2,val3,..." and you want to find rows where 25 is one of those values? If so, use the FIND_IN_SET function:
WHERE FIND_IN_SET('25', `currentMessages`) > 0
OR FIND_IN_SET('25', `removedMessages`) > 0
OR FIND_IN_SET('25', `readMessages`) > 0
...but I really recommend normalizing your data to make querying easier.
You can use FIND_IN_SET for this purpose:
SELECT *
FROM userbulletins
WHERE FIND_IN_SET("25", `currentMessages`)
It would also be worth considering if a different design would be better where you don't have a list of values in a single cell. I recommend that you read this Wikipedia article on first normal form.
That's because IN doesn't work like that. In is for a stated list of values or a list resulting from a subquery.
Either you have to have currentMessages expressed as a string, in which case WHERE currentMessages LIKE '%|25|%' OR currentMessage LIKE '25|%' OR currentMessage LIKE '%|25' OR currentMessage = '25' will work messily, or you have to have another table (or a few of them), such that it's
SELECT * FROM userbulletins
WHERE 25 IN (SELECT * FROM `currentMessages` WHERE....)
--In that case, I'd advise messages(id,[title,content,whatever],status{current,removed,read})
fakeEDIT: The solutions with FIND_IN_SET are better than my LIKE option, but I would still suggest using a table for it.