4
$sql = "SELECT events.*, leagues.lname as leagueName,leagues.aliasname as AliasName, t1.tname as team1_name, t2.tname as team2_name 
FROM events 
INNER JOIN leagues ON events.leagueid = leagues.id 
INNER JOIN teams AS t1 ON events.teamhid = t1.teamid 
INNER JOIN teams AS t2 ON events.teamaid = t2.teamid 
WHERE sdate='1352835000' ";

It gives TABLE with COLUMNS:

id sdate ... leagueName AliasName team1_name team2_name 

How could I add additional conditions:

WHERE AliasName LIKE 'word' AND (team1_name LIKE 'word2' OR  team2_name LIKE 'word3')

2 Answers 2

2

you can't use ALIAS of column on (which is in-line with) where clause, so in your case you need to use the tableName.columnName (aside from wrapping it in a subquery)

WHERE leagues.aliasname LIKE 'word' AND 
     (t1.tname LIKE 'word2' OR  t2.tname LIKE 'word3')

the reason why you can't use is because the order of execution of the query is as follows

  • FROM clause (you can set TABLE alias here)
  • WHERE clause
  • GROUP BY clause
  • HAVING clause
  • SELECT clause (COLUMN alias here)
  • ORDER BY clause
Sign up to request clarification or add additional context in comments.

Comments

1

Like so:

SELECT *
FROM
(
    SELECT 
      events.*, 
      leagues.lname as leagueName,
      leagues.aliasname as AliasName, 
      t1.tname as team1_name, 
      t2.tname as team2_name 
    FROM events 
    INNER JOIN leagues ON events.leagueid = leagues.id 
    INNER JOIN teams AS t1 ON events.teamhid = t1.teamid 
    INNER JOIN teams AS t2 ON events.teamaid = t2.teamid 
    WHERE sdate = '1352835000'
)  t
WHERE AliasName LIKE 'word'
  AND (team1_name LIKE 'word2' OR  team2_name LIKE 'word3')

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.