简体   繁体   中英

MySQL combination of view, subquery and left join produces a strange result

Update 1

I discover when it does the wrong behaviour. If the view is composed by two tables, only the fields in the first table has values inside the subquery. I don't know why, but if I change the JOIN order, it works. As soon as I try to match another field with the second table it returns NULL again.

Update 2

I've created a working example here: http://sqlfiddle.com/#!2/d4eb97/1

Update 3

The same example works in a newer MySQL version (5.6.6) so maybe there is a bug in the 5.5 - http://sqlfiddle.com/#!9/4e140/2


I've a schema in which I ended doing a SQL like this:

SELECT view.user,
(
    SELECT tableA.user
    FROM tableA
    LEFT JOIN tableB ON tableA.id = tableB.tableA_id
    WHERE tableA.user = view.user
    LIMIT 1
) as b_user
FROM view
WHERE view.user = 1

What I'm doing here is simple:

  1. Select two fields from view
    view is a MySQL view, not a real table.
  2. The second field is a subquery of:
    2.1 The field user of the table tableA
    2.2 Left join with the table tableB with the relational field
    There are no rows in tableB yet
    2.3 Only where the the tableA user is the same as in the view
    2.4 Limit 1, just for this example
  3. Limit results to user = 1

The strange thing here is that in some situations the field b_user is NULL , but the data is ok.

I can make three changes to make it works:

fix 1

Put the user id manually make it works

SELECT view.user,
(
    SELECT tableA.user
    FROM tableA
    LEFT JOIN tableB ON tableA.id = tableB.tableA_id
    WHERE tableA.user = 1
    LIMIT 1
) as b_user
FROM view
WHERE view.user = 1

fix 2

Remove the left join also make it works:

SELECT view.user,
(
    SELECT tableA.user
    FROM tableA
    WHERE tableA.user = view.user
    LIMIT 1
) as b_user
FROM view
WHERE view.user = 1

fix 3

Another option is not to use the MySQL view:

SELECT view.user,
(
    SELECT tableA.user
    FROM tableA
    WHERE tableA.user = view_table_a.user
    LEFT JOIN tableB ON tableA.id = tableB.tableA_id
    LIMIT 1
) as b_user
FROM view_table_a INNER JOIN view_table_b ON condition
WHERE table_a.user = 1

I'm not being able to reproduce this recreating a new database schema manually, it only happens in my current setup, which I cannot expose here due to security reasons.

Why the subquery return NULL values? I need to make the first query works since I can't use any of the three fixes.

Why have the subquery in the first place? I like subqueries, they are very handy things of have around. But they shouldn't be used if they don't have to be. Queries can get complicated enough with no help from us.

You are looking for a particular user from the main table (the fact that it is really a view is irrelevant) then using the same User value to join with TableA and then optionally joining to TableB using the ID value associated with that user:

select  rs.Origin, a.Origin as Same_Origin
from    requests_status  rs
join    assignments      a
    on  a.employee = rs.employee
    and a.origin   = rs.origin
left join assignments_author aa
    on  aa.assignment = a.id
where   rs.employee = 1;

Then I noticed that in your fiddles, you create the assignments_author table but never populate it. But that doesn't really matter because you left join to it. But you don't use any data from that table. So in actuality, you don't need that table in your query at all. Thus the equivalent query would be:

select  rs.Origin, a.Origin as Same_Origin
from    requests_status  rs
join    assignments      a
    on  a.employee = rs.employee
    and a.origin   = rs.origin
where   rs.employee = 1;

I don't know why you get a NULL in one but not the other. But since the query above returns the same answer in both fiddles and it is the expected results, my work here is finished.

我认为这是一个错误,也许是一个错误( http://bugs.mysql.com/bug.php?id=52051 ),因为查询在MySQL 5.5中失败( http://sqlfiddle.com/#!2/d4eb97 / 1 ),但可以在5.6( http://sqlfiddle.com/#!9/4e140/2 )中使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM