NYCPHP Meetup

NYPHP.org

[nycphp-talk] MySQL Syntax questions

Tim Lieberman tim_lists at o2group.com
Wed May 28 19:01:36 EDT 2008


On May 28, 2008, at 4:42 PM, Kristina Anderson wrote:

> I have two questions regarding syntax for MySQL queries.
>
> 1.  I have complex queries where there are lots of conditional
> statements, i.e. SELECT * FROM table WHERE this=this AND that=that OR
> thing=thing OR blah=blah OR foo=foo AND bar=bar OR etc=etc OR
> thing2=thing2...
>
>
> They are not erroring out but they aren't narrowing the resultset
> sufficiently...do I need parentheses or is there something else I  
> need?
>

In short: you have to be careful of how expressions are evaluated.   
More parenthesis are likely the solution.


> 2.  How can I select the last row inserted from a table WHERE
> field1=somethingineed and field2 is NULL?

If you've got an auto_increment field (named "id", perhaps):

SELECT max(id) as lastId FROM table where feld1='foo' and field2 is null

-Tim




More information about the talk mailing list