0

Hello,

I have a query that needs to perform the following:

where verified=1
AND
where name is like '%$term%' AND/OR owner is like '%$term%'

I have a basic knowledge of SQL and was wondering if the above is possible.

I need the and/or because i would like it to return a result whether only a name was found, just an owner, or both.


Please help me :)

--Turt2Live

3
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by hielo
0

...i would like it to return a result whether only a name was found, just an owner, or both.

You don't need to AND. The OR should give you what you want even when both are true:

$sql="SELECT `name`, `owner` 
FROM Table WHERE `name` LIKE '$%term%' OR `owner` LIKE '%$term%'";
0

Ok, so if I were to use the following, would it still work?

$sql = "SELECT `name`,`owner` FROM table WHERE `verified`='1' AND name LIKE '$%term%' OR `owner` LIKE '%$term%'";

0

try this

$sql = "SELECT 'name','owner' FROM table WHERE 'verified'='1' AND (name LIKE '$%term%' OR 'owner' LIKE '%$term%')";

Edited by debasisdas: n/a

0

Ok, so if I were to use the following, would it still work?

$sql = "SELECT `name`,`owner` FROM table WHERE `verified`='1' AND name LIKE '$%term%' OR `owner` LIKE '%$term%'";

No! In that specific example, the query is treated as:

...
(`verified`='1' AND name LIKE '$%term%')
OR owner LIKE '%$term%'

In your case you clearly want the OR name with owner, so you must parenthesize that expression:

...`verified`='1' AND 
(name LIKE '$%term%' OR owner LIKE '%$term%')
0

NOTE: In most of these replies change: name LIKE '$%term%' to: name LIKE '%$term%'

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.