I have the following query:
SELECT * FROM questions
LEFT JOIN answers ON (questions.id = answers.id AND (connections.username = answers.username OR connections.username = 'bob' OR answers.username IS NULL))
LEFT JOIN connections ON connections.username1 = 'mikha' AND
(connections.username2 = answers.username)
LEFT JOIN answers answers2 ON (questions.id = answers2.id)
WHERE (answers.id <> answers2.id)
I get the following error:
`Unknown connections.username in ON clause`
I define some conditions in the first join and want to get the rest that don't match these conditions. That's why I use this part answers.id AND answers2.id. To get IDs that don't match the conditions in the first left join.
Thanks
Try it like this, I have no schema to test it myself but I feel like it should work(or something like this)
SELECT * FROM questions, connections
LEFT JOIN answers ON (questions.id = answers.id AND
connections.username2 = answers.username)
where connections.username1 = 'mikha';
eventually like this
SELECT * FROM questions
LEFT JOIN answers ON (questions.id = answers.id)
LEFT JOIN connections ON (connections.username2 = answers.username)
where connections.username1 = 'mikha';
EDIT:
I found this in documentation
Example:
CREATE TABLE t1 (i1 INT);
CREATE TABLE t2 (i2 INT);
CREATE TABLE t3 (i3 INT);
SELECT * FROM t1 JOIN t2 ON (i1 = i3) JOIN t3;
Previously, the SELECT statement was legal. Now the statement fails with an Unknown >column 'i3' in 'on clause' error because i3 is a column in t3, which is not an operand of >the ON clause. The statement should be rewritten as follows:
SELECT * FROM t1 JOIN t2 JOIN t3 ON (i1 = i3);
So for Your case it may be
SELECT * FROM questions
LEFT JOIN connections
LEFT JOIN answers ON (connections.username1 = 'mikha' AND questions.id = answers.id AND
connections.username2 = answers.username)
Effectively if your limiting the results of answers by the results of connections you are doing an inner join not two left joins because one row cannot return with out results from the other because null does not equal null. Given this to be true you can use the following because I have no clue what your schema is all schema was made up to protect the innocent.
SELECT * FROM questions
left join (
select connections.otherstuff,answers.id,answers.text from connections
inner join answers ON connections.username2 = answers.username
where connections.username1 = 'mikha'
) conn on questions.id = conn.id
http://sqlfiddle.com/#!2/c7252/1
Related
I have been struggling with various syntax errors on a query in MS Access that works fine in TSQL.
For example, I learned that MS Access requires parentheses when there are more than one left/inner joins, and also if there is more than one ON condition for any of those joins, thanks to this post and this post.
However, I am now stuck with another "Join expression not supported" error, when I have three ON conditions for an INNER JOIN:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue'
and Table4.fid = Table1.x_id)
)
The above gets the "Join expression not supported" error, but if I remove any one of the three conditions in the last LEFT JOIN ON, the SQL was fine, like this:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.fid = Table1.x_id)
)
Any two of the three conditions was accepted in the above. I have even tried adding additional parentheses like this but it didn't work either:
LEFT JOIN Table4
ON ((Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue')
and Table4.fid = Table1.x_id)
)
Please help - thanks!
I have this query, which works fine:
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
So, it's a query on table 1 with a join on table 2, then subsequent joins from multiple aliased joins of table 3 on table 2, but as soon as I add further joins, I get no results and I'm not sure why, for example:
select table_1.*, coalesce(test_1.type, test_2.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
inner join `table_3` as `test_2` on `test_2`.`code` = `table_2`.`column` and `table_2`.`column` = 'H'
Can anyone explain what I have done wrong?
Try LEFT join on table_3 . If there are no records for table_3, that's why you yield no results, due to the INNER join.
And actually, you're not joining any columns on table_3. Is most likely the issue.
What is the expected sample result of the second of your query ?
Could you please try this query ?
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and
(`table_2`.`column` = 'L' or `table_2`.`column` = 'H')
Usually when I need to join the row with some max value from another column I do something like:
SELECT *
FROM a
INNER JOIN b ON (a.aid = b.aid)
LEFT JOIN b nullb ON (a.aid = nullb.aid AND nullb.value > b.value)
WHERE nullb.bid IS NULL;
I'm not sure if this is the most efficent way but it is a solution without subqueries which i avoid whenever possible.
Today I needed to join in the max value from another table that joins in via a jointable and could not figure out a way to do it.
I tried something like this: (which failed miserably)
SELECT *
FROM a
INNER JOIN atob ON (a.aid = atob.aid)
INNER JOIN b ON (atob.bid = b.bid)
LEFT JOIN atob nullatob ON (a.aid = nullatob.aid)
LEFT JOIN b nullb ON (nullatob.bid = nullb.bid AND nullb.value > b.value)
WHERE nullb.bid IS NULL;
I set up an sqlfiddle at: http://sqlfiddle.com/#!9/86f18/6 with the problem set up.
Anyone got a clever way to join in the max value from another table via a jointable without using subqueries or is that pretty much impossible?
Not sure what is your goal but:
http://sqlfiddle.com/#!9/86f18/13
SELECT A.*, b.*
FROM A
INNER JOIN AtoB
ON (A.Aid = AtoB.Aid)
LEFT JOIN (
SELECT B.*
FROM B
LEFT JOIN b nullB
ON (B.Bid = nullB.Bid AND nullB.bdate > B.Bdate)
WHERE nullB.Bid IS NULL
) b
ON AtoB.Bid = b.Bid
i have this query:
SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS `right_answers_num`
FROM `completed`
INNER JOIN `history` ON `history`.`ID` = `completed`.`ID`
INNER JOIN `questions` ON `questions`.`ID` = `history`.`question`
WHERE `completed`.`student_id` = '1' AND `questions`.`answer` = `history`.`answer`
GROUP BY `completed`.`ID`
ORDER BY `completed`.`completed_in` DESC
what i need is to get info of each test in completed table (id,level,completed_in,right_answer_num)
the problem with that query is that if there is no one right answer(history.answer = questions.answer) then it doesn't return the row, while it should return the row(id,level,completed_in) and the right_answer_num(counter) should be zero..
please help me,, thanks ahead.
SELECT
completed.ID AS ID,
completed.level AS level,
completed.completed_in AS completed_in,
COUNT(questions.answer) AS right_answers_num
FROM completed
INNER JOIN history ON history.ID = completed.ID
LEFT JOIN questions ON questions.ID = history.question AND questions.answer = history.answer
WHERE
completed.student_id = '1'
GROUP BY
completed.ID
ORDER BY completed.completed_in DESC
use a LEFT OUTER JOIN intead of an INNER JOIN.
The second inner join is what's causing rows with no record in the questions table to be omitted. An inner join will only return rows that have data in all corresponding tables. Change the second inner join to a left join like so:
SELECT
completed.ID AS ID,
completed.level AS level,
completed.completed_in AS completed_in,
COUNT(questions.answer) AS right_answers_num
FROM completed
INNER JOIN history ON history.ID = completed.ID
LEFT JOIN questions ON questions.ID = history.question
WHERE completed.student_id = 1
GROUP BY completed.ID
ORDER BY completed.completed_in DESC
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.