Jump to content

Recommended Posts

Hello everyone. I'm trying to run a LEFT JOIN query, it looks as follows:

SELECT tblreservations.*, tblusers.last_name, tblusers.first_name,tblusers.email FROM tblreservations LEFT JOIN tblusers ON (tblreservations.fkUserID=tblusers.userid) WHERE `fkEquipID`='4' AND `timeStart` BETWEEN '2012-03-10' AND '2012-03-17 00:00:00' ORDER BY `timeStart`

I've run this a few times and I keep getting NULL values for the fields from tblusers. I'm not an expert with LEFT JOINs, but I've run a few before (just a week ago I made a query that has four of them, and it works properly). The fields are not set up as foreign keys, but tblusers.userid IS a PK. However as I already mentioned, the query with four LEFT JOINs works just fine to date, and I pretty much copy/pasted that previous query to get this to work. Am I missing something here?

Link to comment
https://forums.phpfreaks.com/topic/258651-issue-with-left-join-query/
Share on other sites

Hello,

Ā 

I considered your suggestions, but as far as troubleshooting goes I've already considered issues comparing a date to a timestamp (no issues from several attempts). I also have tried specifying labreservations.[fieldname] on the WHERE clause but it made no difference. The reservations are properly queried, but the corresponding user data doesn't display. For an explicit example, I tried this (at this point I'm debugging from the command line):

SELECT tblreservations.*, tbladmin.last_name, tbladmin.first_name,tbladmin.email FROM tblreservations LEFT JOIN tbladmin ON (tblreservations.fkUserID=tbladmin.userid) WHERE tblreservations.fkEquipID='4' AND tblreservations.timeStart BETWEEN '2012-03-10 00:00:00' AND '2012-03-17 00:00:00' ORDER BY tblreservations.timeStart;

And this is what I'm given:

+---------+---------------------+---------------------+---------------------+-----------+-----------+----------+------------------+-----------+------------+-------+

| pkResID | dateReservedĀ  Ā  Ā  Ā  | timeStartĀ  Ā  Ā  Ā  Ā  | timeEndĀ  Ā  Ā  Ā  Ā  Ā  | pmtID | fkEquipID | fkUserID | commentsĀ  Ā  Ā  Ā  Ā  | last_name | first_name | email |

+---------+---------------------+---------------------+---------------------+-----------+-----------+----------+------------------+-----------+------------+-------+

|Ā  Ā  Ā  5 | 2012-03-10 01:39:23 | 2012-03-15 05:00:00 | 2012-03-15 05:30:00 |Ā  Ā  Ā  Ā  8 |Ā  Ā  Ā  Ā  4 |Ā  Ā  Ā  66 | Moar testsĀ  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  |

|Ā  Ā  Ā  3 | 2012-03-10 01:35:56 | 2012-03-16 01:00:00 | 2012-03-16 01:30:00 |Ā  Ā  Ā  Ā  2 |Ā  Ā  Ā  Ā  4 |Ā  Ā  Ā  66 | Testing, tooĀ  Ā  | NULLĀ  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  |

|Ā  Ā  Ā  2 | 2012-03-10 01:34:12 | 2012-03-16 01:30:00 | 2012-03-16 02:00:00 |Ā  Ā  Ā  Ā  7 |Ā  Ā  Ā  Ā  4 |Ā  Ā  Ā  66 | testingĀ  Ā  Ā  Ā  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  |

|Ā  Ā  Ā  4 | 2012-03-10 01:38:48 | 2012-03-16 17:00:00 | 2012-03-16 17:30:00 |Ā  Ā  Ā  Ā  7 |Ā  Ā  Ā  Ā  4 |Ā  Ā  Ā  66 | Yet another test | NULLĀ  Ā  Ā  | NULLĀ  Ā  Ā  | NULLĀ  |

+---------+---------------------+---------------------+---------------------+-----------+-----------+----------+------------------+-----------+------------+-------+

However, I can confirm that this fkUserID value exists in tbladmin.

I also tried removing the WHERE clause for a simple join:

SELECT tblreservations.*, tbladmin.last_name, tbladmin.first_name,tbladmin.email FROM tblreservations LEFT JOIN tbladmin ON (tblreservations.fkUserID=tbladmin.userid) ORDER BY tblreservations.timeStart;

And next I removed the ORDER BY clause for a real barebones approach, but the output is the same, all fields from tbladmin come back as 'NULL'.

Ā 

So, I'm going to go by the sticky and add some more data here.

  • MySQL Version: 5.5.21
  • Code is attached, there are no mysql syntax errors

Create statement for tbladmin:

| tbladmin | CREATE TABLE `tbladmin` (
Ā  `userid` int(10) unsigned NOT NULL AUTO_INCREMENT,
Ā  `last_name` varchar(20) NOT NULL,
Ā  `first_name` varchar(20) NOT NULL,
Ā  `email` varchar(255) NOT NULL,
Ā  `phone` varchar(15) DEFAULT NULL,
Ā  `userlevel` tinyint(3) NOT NULL,
Ā  `statusid` tinyint(3) NOT NULL,
Ā  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Ā  PRIMARY KEY (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |

(Some sensitive info edited or omitted)

And for tblreservations:

| tblreservations | CREATE TABLE `tblreservations` (
Ā  `pkResID` int(10) unsigned NOT NULL AUTO_INCREMENT,
Ā  `dateReserved` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Ā  `timeStart` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Ā  `timeEnd` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
Ā  `pmtID` int(10) unsigned NOT NULL,
Ā  `fkEquipID` int(10) unsigned NOT NULL,
Ā  `fkUserID` int(10) unsigned NOT NULL,
Ā  `comments` blob,
Ā  PRIMARY KEY (`pkResID`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |

(Again, some sensitive fields edited or omitted)

UPDATE: after some google-fu, I opted to try using a subquery:

 SELECT tblreservations.*,tbladmin.first_name,tbladmin.last_name,tbladmin.email FROM tblreservations,tbladmin WHERE tblreservations.fkUserID NOT IN (SELECT tbladmin.userid FROM tbladmin);

And it returns exactly what I expect from the LEFT JOIN query (non-null values for the corresponding first_name and last_name). So now I have two follow up questions:

[*]How do I duplicate this result set using the LEFT JOIN query?

[*]Why am I negating tblreservations.fkUserID? Logically, I should think that I'm looking for the result set that matches the one from the IN clause, so what gives?

Sorry for wasting everyone's time. Turns out that I had two tables -- users and tblusers. One was the imported table and the other one was the skeleton of what I was going to modify the original one to. I finally noticed this and the query works like a charmĀ  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.