Pergunta

Here is the query in question:

SELECT e.id, e.name, u.x_account_username, ao.id
FROM x_evidence e
LEFT JOIN x_ambition_owner ao 
ON e.ambition_id = ao.id
LEFT JOIN x_user u
ON ao.profile_id = u.id
WHERE e.updated BETWEEN '2014-03-19 10:16:00' AND '2014-03-19 11:16:00' 

Can anyone see what I'm doing wrong?

Evidence table:

id       ambition_id      name     updated
1jk      2ef              abc      2014-03-19 10:33:31

Ambition owner table:

id       ambition_id        profile_id
1op      2ef                1abc

User table:

id      x_account_username
1abc    rex hamilton

I want my returning to get:

the evidence id, name, user's name, the associated ambition id, between the times stated (but these could vary)

Thanks

Foi útil?

Solução

One way it is not working is that the where clause is turning the first left join into an inner join. So, you would not be getting all rows from the query that you expect. In fact, you might not be getting any of them.

You can fix this by moving the condition into the on clause:

SELECT ao.id AS a_id, e.id AS e_id, e.name AS evidence_name,
       u.x_account_username AS username, 'evidence_edit' AS type
FROM x_ambition_owner ao
LEFT JOIN x_evidence e 
     ON ao.id = e.ambition_id
     AND e.updated BETWEEN '2014-03-19 10:16:00' AND '2014-03-19 11:16:00'
LEFT JOIN x_user u
     ON ao.profile_id = u.id;

EDIT (by OP):

This is the query I will be using:

SELECT e.id, e.name, u.x_account_username AS username, a.ambition_id
FROM x_evidence e
LEFT JOIN x_ambition_owner a
ON e.ambition_id = a.ambition_id
LEFT JOIN x_user u
ON a.profile_id = u.id
WHERE e.updated BETWEEN '2014-03-19 10:00:00' AND '2014-03-19 11:16:00'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top