# How to use JOIN only when value is not 0

**URL:** https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599
**Category:** SQL
**Created:** [March 29, 2022, 10:53pm UTC](https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599 "2022-03-29T22:53:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GM\_sql](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GM\_sql](https://forum.codewithmosh.com/u/GM_sql)
#### Post date: [March 29, 2022, 10:53pm UTC](https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599/1 "2022-03-29T22:53:14Z")

</div>

Hello, could anyone help me to reconstruct the bellow query? (please notice it’s only a part of a bigger query). I’d like to return “0” when banner has value 0, and use Join for other cases.

```auto
SELECT core.name AS author, core.layout, core.banner, core.ad AS ads
    FROM (
        SELECT al.name, ll.name AS layout, p.banner, CASE p.banner WHEN 0 THEN "0" ELSE (JOIN media AS m ON p.banner = m.file_name) END AS ad
        FROM posts AS p
        JOIN authors_look AS al USING(author_id)
        JOIN layouts_look AS ll USING(layout_id)) AS core

```

---

<div class="post-metadata">

### Author: ![ChrisMaringka](https://avatars.discourse-cdn.com/v4/letter/c/8797f3/32.png) [@ChrisMaringka](https://forum.codewithmosh.com/u/ChrisMaringka)
#### Post date: [March 29, 2022, 11:20pm UTC](https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599/2 "2022-03-29T23:20:49Z")

</div>

Hi GM\_sql,

I assume this is the part which you’re wondering of:

CASE p.banner WHEN 0 THEN “0” ELSE (JOIN media AS m ON p.banner = m.file\_name) END AS ad

Could you please share the query result that you are looking for? Sorry but I could not visualize why do you need the Join on that part.

---

<div class="post-metadata">

### Author: ![GM\_sql](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GM\_sql](https://forum.codewithmosh.com/u/GM_sql)
#### Post date: [March 30, 2022, 6:33am UTC](https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599/3 "2022-03-30T06:33:11Z")

</div>

Exactly, this is the part I’m talking about. By using CASE (or IF) I’d like to return any result, now (when banner = 0) I get empty result. It should be:

author layout banner ads  
Tom long 0 “0”  
Jane short 33 “bird.jpg”  
Lisa long 103 “flower.jpg”

---

<div class="post-metadata">

### Author: ![GM\_sql](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GM\_sql](https://forum.codewithmosh.com/u/GM_sql)
#### Post date: [March 30, 2022, 1:44pm UTC](https://forum.codewithmosh.com/t/how-to-use-join-only-when-value-is-not-0/11599/4 "2022-03-30T13:44:00Z")

</div>

Problem already solved. 😀
