Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select statement only pulling part of the information.

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
0
0
GB
Hi

I have two tables

Here is the query:
select * from timesheet as ts left join activities as activ on activ.activity_id = ts.activity_id and date(dateTimeStamp) = '2013-08-08' and user_id = 1 order by startTime desc;

The results are below. The problem I have is, I would like the activities part of the query to pull results not null values(I dont understand why it does not pull the correct values). I do need the timesheet part of the query to pull null values hence the reason I am using a left join.

Any help appreciated.

[pre]
+--------------+---------+-------------+-----------+------+---------------------+---------------------+----------+---------------------+-------------+-------------------------------------------+------------+-------+------------+
| timesheet_id | user_id | activity_id | account | so | startTime | endTime | comments | dateTimeStamp | activity_id | activity | grouporder | reqso | reqcomment |
+--------------+---------+-------------+-----------+------+---------------------+---------------------+----------+---------------------+-------------+-------------------------------------------+------------+-------+------------+
| 1 | 1 | 1 | Client 1 | 9500 | 2013-08-12 12:30:00 | 2013-08-12 14:00:00 | | 2013-08-08 14:14:00 | 1 | Quotes & Sales orders - Prepare or update | a | y | n |
| 2 | 1 | 1 | Client 2 | 9501 | 2013-08-12 11:24:00 | 2013-08-12 12:00:00 | | 2013-08-08 14:14:00 | 1 | Quotes & Sales orders - Prepare or update | a | y | n |
| 3 | 1 | 13 | Client 3 | 9502 | 2013-08-12 10:00:00 | 2013-08-12 11:00:00 | | 2013-08-08 14:14:00 | 13 | Visuals change requests | b | y | n |
| 14 | 1 | 6 | Client 4 | 9541 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-08 11:55:32 | 6 | Project communication with suppliers | a | y | n |
| 13 | 1 | 5 | Client 5 | 9541 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-08 11:33:32 | 5 | Project communication with client | a | y | n |
| 15 | 1 | 6 | Client 6 | 9541 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-08 12:51:05 | 6 | Project communication with suppliers | a | y | n |
| 32 | 1 | 17 | NULL | NULL | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-12 14:56:23 | NULL | NULL | NULL | NULL | NULL |
| 31 | 1 | 17 | NULL | NULL | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-12 14:54:46 | NULL | NULL | NULL | NULL | NULL |
| 30 | 1 | 17 | NULL | NULL | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-12 14:53:33 | NULL | NULL | NULL | NULL | NULL |
| 29 | 1 | 17 | NULL | NULL | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-12 14:52:54 | NULL | NULL | NULL | NULL | NULL |
| 28 | 1 | 17 | NULL | NULL | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | NULL | 2013-08-12 14:48:39 | NULL | NULL | NULL | NULL | NULL |
+--------------+---------+-------------+-----------+------+---------------------+---------------------+----------+---------------------+-------------+-------------------------------------------+------------+-------+------------+
[/pre]

Code:
CREATE TABLE `timesheet` (
  `timesheet_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `activity_id` int(10) NOT NULL,
  `account` varchar(60) DEFAULT NULL,
  `so` int(10) DEFAULT NULL,
  `startTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `endTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `comments` text,
  `dateTimeStamp` datetime NOT NULL,
  PRIMARY KEY (`timesheet_id`)
) ENGINE=MyISAM AUTO_INCREMENT=33 DEFAULT CHARSET=latin1 



CREATE TABLE `activities` (
  `activity_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `activity` varchar(60) NOT NULL,
  `grouporder` varchar(5) DEFAULT NULL,
  `reqso` char(1) DEFAULT NULL,
  `reqcomment` char(1) DEFAULT 'n',
  PRIMARY KEY (`activity_id`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=latin1
 
I have added an attachment image showing the problem. The first example is what I would like to achieve the second is what I am currently getting with my join. Orange headers show the timesheet table and the blue shows the activities table. The green what I would like to see and the red is what I am getting now. Your query will not return row 2.

Thanks again.
 
 http://s13.postimg.org/4b2ijgv13/Query_problem.png
If you change the LEFT JOIN to INNER JOIN (just as an experiment), do those rows disappear from the result set? If so, then for some reason it doesn't think the activity_id (17 in your first example, 5 in your png) has a match. I'm wondering if the field in the timesheet table needs to be unsigned to match the activities table - I wouldn't expect it to be that picky, but since you're having problems, you could try changing timeshare.activity_id to unsigned and see if it makes a difference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top