Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to SELECT 2 tbls but only 1 row from second tbl with lowest PK in MySQL?
    primarykey
    data
    text
    <p>structure:</p> <p>tbl 1</p> <pre><code>|car_id(PK)| make | model | year | ----------------------------------- | 1 | Toyot | Camry | 1999 | | 2 | Honda | Civic | 2005 | </code></pre> <p>tbl 2</p> <pre><code>|img_id(PK)| car_id| img_link | ------------------------------------ | 1 | 1 | tcamry1.jpeg | | 2 | 1 | tcamry2.jpeg | | 3 | 1 | tcamry3.jpeg | | 4 | 2 | hcivic1.jpeg | | 5 | 2 | hcivic2.jpeg | </code></pre> <p>My query:</p> <pre><code>SELECT * FROM cars c LEFT JOIN imgs g ON c.car_id=g.car_id WHERE 1 </code></pre> <p>Result:</p> <pre><code>|img_id(PK)| car_id| make | model | year | img_link | ----------------------------------------------------------- | 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg | | 2 | 1 | Toyot | Camry | 1999 | tcamry2.jpeg | | 3 | 1 | Toyot | Camry | 1999 | tcamry3.jpeg | | 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg | | 5 | 2 | Honda | Civic | 2005 | hcivic2.jpeg | </code></pre> <p>I need to get 1 row for each car and have WHERE clause with something like lowest img_id value out of all img_id related to the same car. </p> <p>Result I want:</p> <pre><code>|img_id(PK)| car_id| make | model | year | img_link | ----------------------------------------------------------- | 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg | | 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg | </code></pre> <p>Thank you.</p> <p><strong>UPDATE:</strong></p> <p>I need something along these lines :-/</p> <pre><code>SELECT g.id, c.car_id, c.mc_make, c.mc_model, c.mc_year, c.mc_desc FROM mycars c INNER JOIN (SELECT * FROM mycars_gallery g WHERE ) ON c.car_id=g.car_id WHERE g.id = min(g.id) </code></pre>
    singulars
    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.
 

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