Re: mysql case statement
| From: | David McGlone | Date: | Mon, 28 Jun 2010 14:27:58 +0000 |
| Subject: | Re: mysql case statement | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-306480@lists.php.net to get a copy of this message | ||
On Monday 28 June 2010 09:49:55 Andrew Ballard wrote:
> On Sun, Jun 27, 2010 at 4:08 AM, Tanel Tammik <keevitaja@gmail.com> wrote:
> > Hello,
> >
> > how to select only if value is present?
> >
> > $query = $db->query("select menus.id, menus.name,
> > case
> > when panels.id is not null then '1'
> > end as hiddenpanel
> >
> > from " . \DB_MENUS . " as menus
> > left join " . \DB_HIDDENPANELS . " as panels on (menus.id =
> > panels.menu_id)
> > where menus.id='" . (int)$id . "'
> > ");
> >
> > i would like to select hiddenpanel only if there is a corresponding value
> > in DB_HIDDENPANELS. At the moment i get NULL if there is no corresponding
> > value in HIDDENPANELS table!
> >
> > Br
> > Tanel
>
> That's what a LEFT JOIN does - it returns all rows from the LEFT table
> that match the criteria in the WHERE clause, and then returns any rows
> from the RIGHT table that happen do match. If you only want rows that
> exist in both tables, change the join from LEFT (OUTER) JOIN to INNER
> JOIN.
Tanel, we both learned something. I didn't fully understand join myself yet,
but I think I do now.
but let me ask this if the join wasn't there would an if statement like I
mentioned have worked?
Blessings,
David M.