1

Just wondering if I'm missing something here:

$sql = "SELECT DISTINCT usr.id, 
CONCAT(usr.username, '".PHP_EOL."' ) as username 
FROM #__ads AS a 
RIGHT JOIN #__users 
WHERE gid= '19' AS usr  
    ON usr.id = a.userid 
ORDER BY usr.username";

It works when I remove the WHERE gid = '19' part, is it in the wrong place? The gid column only exists in the __users table.

Thank you!

Edit: got it, just needed to put the Where clause right before the ORDER BY.

2 Answers 2

1

The where clause must come after any joins:

$sql = "SELECT DISTINCT usr.id
             , CONCAT(usr.username, '".PHP_EOL."' ) as username
        FROM #__ads AS a
        RIGHT JOIN #__users AS usr
        ON usr.id = a.userid 
        WHERE gid= '19'
        ORDER BY usr.username";
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want just the row(s) from #__users where usr.gid= '19'

SELECT DISTINCT usr.id, 
                CONCAT(usr.username, '".PHP_EOL."' ) as username 
FROM #__ads AS a 
RIGHT JOIN #__users as usr  ON usr.id = a.userid  
WHERE usr.gid= '19' 
ORDER BY usr.username

The alternative

SELECT DISTINCT usr.id, 
                CONCAT(usr.username, '".PHP_EOL."' ) as username 
FROM #__ads AS a 
RIGHT JOIN #__users as usr  ON usr.id = a.userid  
                            AND usr.gid= '19' 
ORDER BY usr.username

Would give you all rows from #__users but only join on those matching usr.gid= '19'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.