sql query

Lou Kamenov lou.kamenov at aeye.net
Wed Nov 13 17:18:05 GMT 2002

In some email I received from Paul Robinson <paul at iconoplex.co.uk> on
Wed, 13 Nov 2002 15:59:29 +0000 :
> On Nov 13, Robin Garbutt <rob at portfoliodesign.net> wrote:
> > can you use 'LIKE' and '=' in the same sql query?
> Yes.
> > e.g
> > 
> > if ($result = mysql_query("SELECT * FROM db WHERE name LIKE '$name%'
> > OR id ='$id'", $db));
> > {
> >     blah blah blah
> > }
> > 
> > as the id don't seem to be querying...
> > 
> > any suggestions?
> id won't be checked if a result is returned on the LIKE - i.e., a name
> matches, so $id won't be required because of the OR. If the id matches
> you're expecting that don't match LIKE $name aren't coming back try
> putting braces around each part of your query - WHERE (name LIKE
> '$name%') OR (id ='$id') - and perhaps switching it around might help.
No, it will do both of them, this a boolean, it will output results for
'like '$name%', 'id='$id'. Nothing will change even if
you switch them:)
With AND it will look different.

I think a subselect might solve this issue, though mysql doesnt
have subselects.[*]
take a look at this,
Although if $id is an int, no need to put it in ''.

[*] I prefer PostgreSQL, it has all the goodies(like subselects,
triggers etc etc) that mysql doesnt, though mysql is lighter than pgsql.



Lou Kamenov	lou at freebsd-bg.org		lou at seclab.aeye.net
FreeBSD BGUG	http://www.freebsd-bg.org	http://www.aeye.net
Key Fingerprint - 936F F64A AD50 2D27 07E7  6629 F493 95AE A297 084A
It`s a free ride when you`ve already paid.

More information about the Ukfreebsd mailing list