Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL INNER JOIN. Pull data from one table with data from another
    primarykey
    data
    text
    <p>I am just getting started in learning how to do INNER JOINS correctly and I can't think of the best/easiest way to do this.</p> <p>I am building a url shortener and I am trying to build a query that will get all long_url.destination's matching a slug "test". One slug might point to multiple long_url.destination's(URL shuffling, GEO matching, etc...). So I need the slug to get all long_url.destination's with the same short_url.slug.</p> <p>Before I was running another query to get the short_id from the slug, then running another query to select all rows in long_url that had a matching short_id. </p> <p>I think it might be quicker if I use an inner join, but I am unsure how to properly set it up. </p> <p>I want to get all destination columns in table long_url with only the slug data in short_url without having to run a separate query to get the short_id from the slug.</p> <pre><code>Table: short_url Columns: short_id | slug | enabled | timestamp example: 1 test 1 1323343922 Table: long_url Columns: long_id | short_id | destination | geo | enabled | timestamp example: 1 1 http://www.test.com US 1 132334922 example: 2 1 http://www.test.co.uk UK 1 132334922 </code></pre> <p>I got this so far:</p> <pre><code>SELECT destination, geo FROM long_url INNER JOIN short_url ON long_url.short_id = short_url.short_id WHERE enabled = 1; function get_long_urls($slug) { $query = "SELECT...."; $stmt = $db-&gt;prepare($query); $stmt-&gt;execute(array(':slug' =&gt; $slug)); $results = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); return (array) $results: } example $results = array( 'http://www.test.com' =&gt; 'US', 'http://www.test.co.uk' =&gt; 'UK', ); </code></pre> <p>Thanks for any help.</p>
    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.
 

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