Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating a MySQL Query 2

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
I have the key phrase "Monkey Nuts" that I want to search for.
I have two fields:
In field [animal] I have a value "Monkey"
In field [food] I have a value "Nuts"

I want to search a table for [animal] + ' ' [food], so it returns "Monkey Nuts".

So I tried:

Code:
$query = "SELECT * FROM mytable WHERE (animal, + ' ' + food) = 'Monkey Nuts'";

It doesn't work. What am I doing wrong?
 
Use either the string concatenator (||) or the CONCAT function. But beware:
- the standard string concatenator is by default seen as a boolean OR if you do not configure the server to adapt the standards (ANSI mode).
- concatenating any field with value NULL will cause the result to be NULL. Use the IFNULL function if you want the concatenation of an empty string in such a case.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Code:
SELECT * FROM mytable WHERE CONCAT(field1,' ',field2) = 'Monkey Nuts'


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks to DonQuichote and vacunita.
The function CONCAT was what I needed and you both guided me in the right direction. Easy when you know how!

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top