How can I use OR operator with 2 tables? - mysql

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%")

Related

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.

MySQL return no results from select * where varchar="" query

I'm having issue with my study project for creating database in MySQL.
I've imported data using LOAD to my created table from a CSV file. Now when I'm executing select * from mytable everything show up perfectly, but when I execute select * from bi_jogging.routes as r where r.Creator_Email="jhenderson7c#bbb.org"
I get nothing.
The email is there, I've tried different syntax options but it seems to be something else, I suspect something with the varchar format, I really have nothing in mind.
For other tables it works fine and others not.
You can try using the query:
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
If like operator shows the result then there may be white spaces in the email, please double check..
For join try this:
select * from bi_jogging.routes as r join bi_jogging.buddies as b
on b.Email like '%r.Creator_Email%'
I think it should work. Again check with same code.
select * from bi_jogging.routes as r where r.Creator_Email='jhenderson7c#bbb.org'
if [select * from mytable] this works ,then try to copy the email from result and paste it in where clause.
There may be conflicts between quotes.
your table entry contains quotes???
check properly. i think you have quotes in your table entry,so when you try this,
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
'%' sign matches with any character and you will get the result.
Inside the tablejhenderson7c#bbb.org and "jhenderson7c#bbb.org" are completely different.
I found spaces in the mysql tables after few emails, so I guess that was it. burned 8 hours on this one, thank you all. I could not find the spaces at the end of the mail by looking at it, I had to hit backspace to see that only after two hits the last char is deleted
this helped me : UPDATE bi_jogging.results set Mail_Found = TRIM(Replace(Replace(Replace(Mail_Found,'\t',''),'\n',''),'\r',''));

Search by tags and title in mysql

I want to search in my db programs and news by title or tags I have this code in mysql:
Select *
from promociones,promocion_tag,programa_tag,tags,programas
WHERE tags.nombre='STH'
AND tags.id=programa_tag.id_tag
AND programa_tag.id_programa=programas.id
OR tags.nombre='STH'
AND tags.id=promocion_tag.id_tag
AND promocion_tag.id_promocion=promociones.id
OR promociones.titulo LIKE "%STH%"
OR programas.titulo LIKE "%STH%"
You see any error? Because its return something wrong, the same row many times..
Tables (important columns):
Programas
-ID
-Titulo
Promociones
-ID
-Titulo
-Tags
-ID
-Nombre
Programa_tag
-id_tag
-programa_tag
Promocion_tag
-id_tag
-promocion_tag
You need to use parentheses due to the precedence of AND then OR. Using explicit JOIN syntax would also be helpful.
...
AND tags.id=programa_tag.id_tag
AND (programa_tag.id_programa=programas.id
OR tags.nombre='STH')
AND tags.id=promocion_tag.id_tag
AND (promocion_tag.id_promocion=promociones.id
OR promociones.titulo LIKE "%STH%"
OR programas.titulo LIKE "%STH%")
See the documentation
For better readability (and easier debugging) I've rephrased your SELECT to use JOINS (plus I added parenthesis around the OR expression:
SELECT * FROM tags
JOIN programa_tag ON tags.id=programa_tag.id_tag
JOIN programas ON programa_tag.id_programa=programas.id
JOIN promocion_tag ON promocion_tag.id_tag = tags.id
JOIN promociones ON promociones.id = promocion_tag.id_promocion
WHERE tags.nombre='STH' AND
(promociones.titulo LIKE "%STH%" OR programas.titulo LIKE "%STH%")
Without further knowledge about your database I can only guess if the JOINs should actually be LEFT JOINS or not. Also, this SELECT might actually require a UNION, but you'd need to give more details to make that call.

LEFT JOIN breaks WHERE Clause

I've recently been required to input more information from my database and I've just LEFT JOIN to help me, it works almost perfectly(it does actually get the right field from the other table) but my WHERE clause is nullified giving the user access to both tables without the restriction of my where clause.
MySQL doesn't crap out any errors, so I'm assuming it's something to do with my where clause or something happened in the join.
SELECT * FROM students
LEFT JOIN courses ON students.appliedforCourse = courses.idNumber
WHERE
students.telephone LIKE '%$var'
OR students.email LIKE '%$var'
OR students.address like'%$var%'
OR (CONCAT(students.firstName,' ',students.lastName) LIKE '%$var%')
AND addedBy ='$userid'
LIMIT $s,limit
The query itself is correct (although really inefficient due to ORs and % % [ indexes will not be used] ).
I would suggest to echo the query, are you sure that $var is evaluated correctly ? Try to run the query directly in mysql (via phpmyadmin for example or using console).
I suspect that simply you did not set $var value. Then condition e.g. students.telephone LIKE '%$var' will become students.telephone LIKE '%' (always true for not null address), which will match every record of the join , exactly what you are getting.

mysql group_concat in where

I am having a problem with the following query(if this is a duplicate question then i'm terribly sorry, but i can't seem to find anything yet that can help me):
SELECT d.*, GROUP_CONCAT(g.name ORDER BY g.name SEPARATOR ", ") AS members
FROM table_d AS d LEFT OUTER JOIN table_g AS g ON (d.eventid = g.id)
WHERE members LIKE '%p%';
MySQL apparently can't handle a comparison of GROUP_CONCAT columns in a WHERE clause.
So my question is very simple. Is there a workaround for this, like using sub-query's or something similar? I really need this piece of code to work and there is not really any alternative to use other than handling this in the query itself.
EDIT 1:
I won't show the actual code as this might be confidential, I'll have to check with my peers. Anyway, I just wrote this code to give you an impression of how the statement looks like although I agree with you that it doesn't make a lot of sense. I'm going to check the answers below in a minute, i'll get back to you then. Again thnx for all the help already!
EDIT 2:
Tried using HAVING, but that only works when i'm not using GROUP BY. When I try it, it gives me a syntax error, but when I remove the GROUP BY the query works perfectly. The thing is, i need the GROUP BY otherwise the query would be meaningless to me.
EDIT 3:
Ok, so I made a stupid mistake and put HAVING before GROUP BY, which obviously doesn't work. Thanks for all the help, it works now!
Use HAVING instead of WHERE.
... HAVING members LIKE '%peter%'
WHERE applies the filter before the GROUP_CONCAT is evaluated; HAVING applies it later.
Edit: I find your query a bit confusing. It looks like it's going to get only one row with all of your names in a single string -- unless there's nobody in your database named Peter, it which case the query will return nothing.
Perhaps HAVING isn't really what you need here...
Try
SELECT ...
...
WHERE g.name = 'peter'
instead. Since you're just doing a simple name lookup, there's no need to search the derived field - just match on the underlying original field.
GROUP_CONCAT is an aggregate function. You have to GROUP BY something. If you just want all the rows that have %peter% in them try
SELECT d.*, g.name
FROM table_d AS d
LEFT OUTER JOIN table_g AS g
ON (d.eventid = g.id)
WHERE g.name LIKE '%peter%';