Bug #60459 | Using default values of table fields when we do a left join table. | ||
---|---|---|---|
Submitted: | 14 Mar 2011 12:07 | Modified: | 14 Mar 2011 16:50 |
Reporter: | Jerzy Kruszyna-Kotulski | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Options | Severity: | S5 (Performance) |
Version: | OS: | Linux (Ubuntu 10.04) | |
Assigned to: | CPU Architecture: | Any | |
Tags: | default values, left join, using default values of fields |
[14 Mar 2011 12:07]
Jerzy Kruszyna-Kotulski
[14 Mar 2011 12:37]
Valeriy Kravchuk
Sorry, but I do not follow you. I get the following results from your query: mysql> select * from tests_definitions -> inner join tests_status -> on tests_definitions.test_id = tests_status.t est_id -> left join tests -> on tests_status.test_number = tests.test_numb er -> where test_status = 1 -> ; +---------+----------+--------------+-------------------------+-------------+--- ------+-------------+----------+---------+--------------+-------------+ | test_id | point_id | name | data | test_number | te st_id | test_status | point_id | answare | point_status | test_number | +---------+----------+--------------+-------------------------+-------------+--- ------+-------------+----------+---------+--------------+-------------+ | 1 | 1 | test point 1 | test point 1 dfgsdfgdfg | 1 | 1 | 1 | NULL | NULL | NULL | NULL | | 1 | 2 | test point 2 | asdc sadf sadf sd | 1 | 1 | 1 | NULL | NULL | NULL | NULL | | 1 | 3 | test point 3 | adsASD SADF SADF SD | 1 | 1 | 1 | NULL | NULL | NULL | NULL | | 1 | 4 | test point 4 | SDAFG L.FNSDLFG DFG | 1 | 1 | 1 | NULL | NULL | NULL | NULL | +---------+----------+--------------+-------------------------+-------------+--- ------+-------------+----------+---------+--------------+-------------+ 4 rows in set (0.06 sec) What other result you expect or would like to get?
[14 Mar 2011 14:27]
Valeriy Kravchuk
I do not think that we need any new syntax for this. Consider the following simplified example: macbook-pro:5.5 openxs$ bin/mysql -uroot test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 5.5.11-debug Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create table t1(c1 int default 0, c2 int default 1); Query OK, 0 rows affected (0.07 sec) mysql> create table t2(c1 int default 0, c2 int default 1); Query OK, 0 rows affected (0.13 sec) mysql> insert into t1 values(1,1); Query OK, 1 row affected (0.00 sec) mysql> select * from t1 left join t2 on t1.c1 = t2.c1; +------+------+------+------+ | c1 | c2 | c1 | c2 | +------+------+------+------+ | 1 | 1 | NULL | NULL | +------+------+------+------+ 1 row in set (0.00 sec) If you want to use default value of, say, column c2 from table t2 when it is NULL (as a result of left join), you can use a combination of IFNULL() and DEFAULT() functions: mysql> select t1.*, t2.c1, ifnull(t2.c2, default(t2.c2)) as t2c2 from t1 left join t2 on t1.c1 = t2.c1; +------+------+------+------+ | c1 | c2 | c1 | t2c2 | +------+------+------+------+ | 1 | 1 | NULL | 1 | +------+------+------+------+ 1 row in set (0.03 sec)
[14 Mar 2011 15:17]
Valeriy Kravchuk
Looks like it's time to ask: what exact version of MySQL server, x.y.z, do you use?
[14 Mar 2011 16:13]
Valeriy Kravchuk
Well, this is what I get: mysql> SELECT tests_definitions.*, -> tests_status.*, -> ifnull(tests.point_status, 0 ) as point_status -> FROM tests_definitions -> INNER JOIN tests_status ON tests_definitions.test_id = tests_status.test_id -> LEFT JOIN tests ON tests_status.test_number = tests.test_number -> WHERE test_status =1 -> ; +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ | test_id | point_id | name | data | test_number | test_id | test_status | point_status | +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ | 1 | 1 | test point 1 | test point 1 dfgsdfgdfg | 1 | 1 | 1 | 0 | | 1 | 2 | test point 2 | asdc sadf sadf sd | 1 | 1 | 1 | 0 | | 1 | 3 | test point 3 | adsASD SADF SADF SD | 1 | 1 | 1 | 0 | | 1 | 4 | test point 4 | SDAFG L.FNSDLFG DFG | 1 | 1 | 1 | 0 | +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ 4 rows in set (0.08 sec) NULL is replaced by 0 as expected, because there is no row in tests that correspond to other table. Now, I add one: mysql> insert into tests values(1,'aaa',1,1); Query OK, 1 row affected (0.07 sec) Note that it will correspond to all 4 tuples from other tables, thus the result: mysql> SELECT tests_definitions.*, tests_status.*, ifnull(tests.point_status, 0 ) as point_status FROM tests_definitions INNER JOIN tests_status ON tests_definitions.test_id = tests_status.test_id LEFT JOIN tests ON tests_status.test_number = tests.test_number WHERE test_status =1; +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ | test_id | point_id | name | data | test_number | test_id | test_status | point_status | +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ | 1 | 1 | test point 1 | test point 1 dfgsdfgdfg | 1 | 1 | 1 | 1 | | 1 | 2 | test point 2 | asdc sadf sadf sd | 1 | 1 | 1 | 1 | | 1 | 3 | test point 3 | adsASD SADF SADF SD | 1 | 1 | 1 | 1 | | 1 | 4 | test point 4 | SDAFG L.FNSDLFG DFG | 1 | 1 | 1 | 1 | +---------+----------+--------------+-------------------------+-------------+---------+-------------+--------------+ 4 rows in set (0.00 sec) value 1 in every row. I do not see any bug here, sorry.
[14 Mar 2011 16:43]
Jerzy Kruszyna-Kotulski
You have right, I make a change in query to this one: SELECT tests_definitions.*, tests_status.*, ifnull(tests.point_status, 0 ) as point_status FROM tests_definitions INNER JOIN tests_status ON tests_definitions.test_id = tests_status.test_id LEFT JOIN tests ON tests_definitions.point_id = tests.point_id WHERE test_status = 1 And now it's work like I need. Brilliant wokr Valeriy, thanks a lot for your patience and time Jerzy ps. It's not a bug.