Search code examples
mysqlleft-joinmultiple-databases

MYSQL query from 2 DBs gives NULLs for second DB


This is continuing story from this PHP mysql LEFT JOIN output

I have now done modifications to second database (Cu.data) and database tables are like this:

mysql> desc ins.data;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| ID                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| Date              | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| Number            | text             | NO   |     | NULL                |                |
| Text              | text             | NO   |     | NULL                |                |
| Email             | text             | NO   |     | NULL                |                |
| TargetID          | varchar(20)      | NO   |     | NULL                |                |
| CSW               | text             | NO   |     | NULL                |                |
| TSW               | text             | NO   |     | NULL                |                |
| Key               | text             | NO   |     | NULL                |                |
| CType             | text             | NO   |     | NULL                |                |
+-------------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.00 sec)

mysql> desc Cu.data;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| Title    | decimal(15,0) | NO   |     | NULL    |       |
| Cu       | varchar(45)   | NO   |     | NULL    |       |
| Co       | varchar(25)   | NO   |     | NULL    |       |
| Ci       | varchar(25)   | NO   |     | NULL    |       |
| SID      | int(12)       | NO   |     | NULL    |       |
| TargetID | varchar(20)   | NO   | MUL | NULL    |       |
| SType    | varchar(12)   | NO   |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

My query qoes like this:

mysql> SELECT ins.data.Date,  ins.data.Number,  
ins.data.Email, ins.data.TargetD, ins.data.CSW, 
ins.data.TSW, ins.data.CType, Cu.data.Cu, 
Cu.data.SID, Cu.data.Co,
Cu.data.Ci, Cu.data.SType 
FROM ins.data 
LEFT JOIN  
Cu.data ON (ins.data.TargetID = Cu.data.TargetID);

And output shows all needed values from ins.data (first database) BUT all values from Cu.data are 'NULL'

TargetID is the 'link' between databases. ie. with TargetID I can map data between databases.

Data on ins.data is increasing slowly when peoples inserts data into it. Cu.data is static, it will be updated maybe once a year.

Since i never got PHP working i decided to utilize my bash programming skills. Via cron script sends email with fetched data from databases. Scripts works perfectly, but some data is missing.

And now the question; Why on earth my SQL-clause outputs data only from ins.data !?


Solution

  • When investigating join condition issues, the first place to look is whether the columns are compatible with each other (i.e. same type and storage size).

    For text columns there may also be issues with spaces and non-printable characters that may not be found easily.