title: SQL: LEFT JOIN and alias not working together
body: <pre><code>$query = "SELECT a.comment_user_id as main_id, a.comment_date as timestamp, a.comment_content as content, a.comment_link_id as link_unique, a.comment_id as status, NULL as url, b.user_login as ulogin, b.user_avatar_source as uavatar, c.link_title as ltitle, NULL as desc FROM kliqqi_comments as a WHERE comment_user_id IN ('$following2')  
          LEFT JOIN kliqqi_users as b ON a.comment_user_id = b.user_id  
          LEFT JOIN kliqqi_links as c ON a.comment_user_id = c.link_author  
      ORDER BY timestamp DESC LIMIT 10";  
  
  $result = mysqli_query($db_conx, $query);
  $row = $result-&gt;fetch_array(MYSQLI_ASSOC);
  </code></pre>
  
  <p>Can anybody tell me what's wrong with the code? It is always returning this error: </p>
  
  <blockquote>
    <p>Fatal error: Call to a member function fetch_assoc() on boolean</p>
  </blockquote>
  
  <p>Boolean means this query is not getting executed due to some error in $query variable which I am unable to figure out.</p>
  
  <p>$following is an array. kliqqi_comments alias a, kliqqi_users alias b, kliqqi_links alias c. I am storing all the other fields as alias too. There is no typo or any other silly mistake. I've checked it thoroughly.</p>
  
  <p><strong>UPDATE:</strong>
  I'm updating this thread because my query actually has many parts and many users may find it helpful.</p>
  
  <pre><code>$query = "SELECT a.comment_user_id as main_id, a.comment_date as timestamp2, a.comment_content as content, a.comment_link_id as link_unique, a.comment_id as status, b.user_login as ulogin, b.user_avatar_source as uavatar, c.link_title as ltitle FROM kliqqi_comments a
                  LEFT JOIN kliqqi_users b ON a.comment_user_id = b.user_id
                  LEFT JOIN kliqqi_links c ON a.comment_link_id = c.link_id
                  WHERE comment_user_id IN ('$following')
                  UNION ALL
                SELECT d.link_author as main_id, d.link_date as timestamp2, d.link_status as content, d.link_id as link_unique, NULL as status, e.user_login as ulogin, e.user_avatar_source as uavatar, d.link_title as ltitle FROM kliqqi_links d
                  LEFT JOIN kliqqi_users e ON d.link_author = e.user_id
                  WHERE link_author IN ('$following') AND link_status IN ('new','published')
                  UNION ALL
                SELECT f.vote_user_id as main_id, f.vote_date as timestamp2, f.vote_value as content, f.vote_link_id as link_unique, NULL as status, g.user_login as ulogin, g.user_avatar_source as uavatar, h.link_title as ltitle FROM kliqqi_votes f
                  LEFT JOIN kliqqi_users g ON f.vote_user_id = g.user_id
                  LEFT JOIN kliqqi_links h ON f.vote_link_id = h.link_id
                  WHERE vote_user_id IN ('$following')
                ORDER BY timestamp2 DESC LIMIT 30";
  </code></pre>
  
  <p><strong>What does it do?</strong></p>
  
  <p>I've 3 tables: kliqqi_links, kliqqi_users, kliqqi_votes</p>
  
  <blockquote>
    <p>UNION ALL</p>
  </blockquote>
  
  <p>All of them have a timestamp field.
  I wanted to fetch contents from these 3 tables combined in decreasing order of timestamp. And to do so, I used UNION ALL (UNION can also be used here but UNION has to run duplicate checks so it's better to avoid it if you can.). But UNION ALL works only when all of the tables have same number of fields. So, I created NULL elements for equating the numbers.
  It is to be noted that there is no restriction of datatype for uniting respective fields. But since I had to use timestamp for sequence, I kept them together.</p>
  
  <blockquote>
    <p>Alias</p>
  </blockquote>
  
  <p>Since all the respective fields have different names in different tables, I used alias to avoid confusion. Without alias, results are stored in fields mentioned in first SELECT statement which would be a mess.</p>
  
  <blockquote>
    <p>Multiple LEFT JOIN</p>
  </blockquote>
  
  <p>Now, I wanted to grab some data from other tables for each SELECT query.
  e.g. for <strong>kliqqi_comments</strong> (first SELECT statement), I wanted to grab user data for the person who made the comment from <strong>kliqqi_users</strong> plus I wanted to fetch the link where this comment was made from <strong>kliqqi_links</strong> table. So, I used left join with <strong>kliqqi_comments</strong> query where <em>comment_user_id</em> from <strong>kliqqi_comments</strong> equals to <em>user_id</em> from <strong>kliqqi_users</strong> and <em>comment_link_id</em> from <strong>kliqqi_comments</strong> equals <em>link_id</em> from <strong>kliqqi_links</strong>.
  Notice that I managed to equate fields in all 3 statements for UNION ALL.</p>
  
  <blockquote>
    <p>WHERE IN</p>
  </blockquote>
  
  <p>$following is comma separated array to ensure that it returns result from the people user is following.</p>
  
  <blockquote>
    <p>ORDER BY DESC, LIMIT</p>
  </blockquote>
  
  <p>To order by timestamp and LIMIT output result.</p>
  
  <p>That's it.</p>