Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are mixing implicit joins with explicit joins. That is allowed, but you need to be aware of how to do that properly.</p> <p>The thing is, explicit joins (the ones that are implemented using the <code>JOIN</code> keyword) take precedence over implicit ones (the 'comma' joins, where the join condition is specified in the <code>WHERE</code> clause).</p> <p>Here's an outline of your query:</p> <pre><code>SELECT … FROM a, b LEFT JOIN dkcd ON … WHERE … </code></pre> <p>You are probably expecting it to behave like this:</p> <pre><code>SELECT … FROM (a, b) LEFT JOIN dkcd ON … WHERE … </code></pre> <p>that is, the combination of tables <code>a</code> and <code>b</code> is joined with the table <code>dkcd</code>. In fact, what's happening is</p> <pre><code>SELECT … FROM a, (b LEFT JOIN dkcd ON …) WHERE … </code></pre> <p>that is, as you may already have understood, <code>dkcd</code> is joined specifically against <code>b</code> and only <code>b</code>, then the result of the join is combined with <code>a</code> and filtered further with the <code>WHERE</code> clause. In this case, any reference to <code>a</code> in the <code>ON</code> clause is invalid, <code>a</code> is unknown at that point. That is why you are getting the error message.</p> <p>If I were you, I would probably try to rewrite this query, and one possible solution might be:</p> <pre><code>SELECT DISTINCT a.maxa, b.mahuyen, a.tenxa, b.tenhuyen, ISNULL(dkcd.tong, 0) AS tongdkcd FROM phuongxa a INNER JOIN quanhuyen b ON LEFT(a.maxa, 2) = b.mahuyen LEFT OUTER JOIN ( SELECT maxa, COUNT(*) AS tong FROM khaosat WHERE CONVERT(datetime, ngaylap, 103) BETWEEN 'Sep 1 2011' AND 'Sep 5 2011' GROUP BY maxa ) AS dkcd ON dkcd.maxa = a.maxa WHERE a.maxa &lt;&gt; '99' ORDER BY a.maxa </code></pre> <p>Here the tables <code>a</code> and <code>b</code> are joined first, then the result is joined to <code>dkcd</code>. Basically, this is the same query as yours, only using a different syntax for one of the joins, which makes a great difference: the reference <code>a.maxa</code> in the <code>dkcd</code>'s join condition is now absolutely valid.</p> <p>As @Aaron Bertrand has correctly noted, you should probably qualify <code>maxa</code> with a specific alias, probably <code>a</code>, in the <code>ORDER BY</code> clause.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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