Note that there are some explanatory texts on larger screens.

plurals
  1. POGet undefined join values
    primarykey
    data
    text
    <p>I have 3 tables: a shop table, a stock table, and a stock_amounts table. The shop table defines each shop's details, the stock tables defines all stock items, and the stock_amounts table defines how much stock each shop has of an item.</p> <p>My tables look like this:<br></p> <pre><code>*Shop table: | id | shop ------------- | 1 | Head Office | 2 | shop 1 *Stock table: | id | stock_description ------------------------ | 1 | Some description | 2 | Some description | 3 | Some description *Stock amounts table: | id | shop_id | stock_id | stock_amount ------------------------------------------ | 1 | 1 | 1 | 5 | 2 | 2 | 2 | 4 </code></pre> <p>I want to display all the stock that a shop has, but also display the stock that hasn't been defined for that shop. For example, shop 1 has 5 of stock 1, but none of stock 2 or stock 3. I want stock 2 and 3 to display as either zero or NULL value.</p> <p>I'm currently joining it all together. If I do not specify anything in the <code>WHERE</code> clause, all shops with their stock is shown, and NULL is shown for undefined values. If I do <code>WHERE stock_amounts.id &lt;&gt; '2'</code>, then all NULL values no longer show.</p> <p>What I want to know:<br> 1) Why does NULL no longer show if I specify 'not 2' in the WHERE clause?<br> 2) What is the proper way of doing this, and what would the right MySQL query look like? <br></p> <h2>Edit:</h2> <p>To be clear, I have no problem whatsoever getting which shop has which stock. The problem I'm having is showing all the stock that <b>shop 1</b> has + all the stock <b>shop 1</b> <i>does not</i> have. <br> My query:<br> <code>SELECT shop, stock.id stock_id, description, amount</code><br> <code>FROM stock</code><br> <code>LEFT JOIN shop_stock ON shop_stock.stock_id = stock.id</code><br><br> This query returns:</p> <pre><code>|shop|stock_id|description |amount -------------------------------------- |1 |1 |Some description|5 |2 |2 |Some description|4 |NULL|3 |Some description|NULL </code></pre> <p>If I add <code>WHERE shop &lt;&gt; '2'</code>, the NULL also disappears. I want a query that will return the following table for <b>shop 1</b>:</p> <pre><code>|shop|stock_id|description |amount -------------------------------------- |1 |1 |Some description|5 |NULL|2 |Some description|NULL |NULL|3 |Some description|NULL </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload