mysql not equal don't work
I have a SELECT statement that looks like this:
SELECT *
FROM photos, p_votes
WHERE p_votes.p_id = photos.p_id
AND p_votes.u_id = 237
And returns me results which I want, but I want just other way around: "I want all results that are not like above". So I was doing like this:
SELECT *
FROM photos, p_votes
WHERE p_votes.p_id != photos.p_id
AND p_votes.u_id != 237
but it doesn't return any results.
开发者_Go百科Here is my insertion code:
$query = "UPDATE photos set p_up=p_up+1 where p_id=".$p_id;
$result = $this->db->query($query);
$this->load->helper('date');
$now = time();
$data_p_votes = array(
'p_id' => $p_id,
'u_id' => $this->session->userdata('u_id'),
'pv_ip' => $ip,
'pv_date' => unix_to_human($now, TRUE, 'eu')
);
$query_p_votes = $this->db->insert('p_votes', $data_p_votes);
I just want to get photos that haven't been voted on by the user.
Here is my database structure and some sample records:
CREATE TABLE IF NOT EXISTS `photos` (
`p_id` bigint(20) unsigned NOT NULL auto_increment,
`u_id` bigint(20) unsigned NOT NULL default '0',
`p_date` datetime NOT NULL default '0000-00-00 00:00:00',
`p_content` longtext NOT NULL,
`p_title` text NOT NULL,
`p_photo` text NOT NULL,
`p_small` text NOT NULL,
`p_thumb` text NOT NULL,
`p_up` bigint(20) default '0',
`p_down` bigint(20) default '0',
PRIMARY KEY (`p_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=260 ;
INSERT INTO `photos` (`p_id`, `u_id`, `p_date`, `p_content`, `p_title`, `p_photo`, `p_small`, `p_thumb`, `p_up`, `p_down`) VALUES
(255, 237, '2010-12-15 16:00:48', 'dock', 'dock', 'application/uploads/237/1292425240Dock.jpg', 'application/uploads/237/Jahorina-1292425240Dock.jpg', 'application/uploads/237/1292425240Dock_thumb.jpg', 1, 0),
(254, 237, '2010-12-15 16:00:23', 'desert', 'desert', 'application/uploads/237/1292425214Desert Landscape.jpg', 'application/uploads/237/Jahorina-1292425214Desert Landscape.jpg', 'application/uploads/237/1292425214Desert Landscape_thumb.jpg', 1, 0)
CREATE TABLE IF NOT EXISTS `p_votes` (
`pv_id` bigint(20) unsigned NOT NULL auto_increment,
`u_id` bigint(20) unsigned NOT NULL,
`p_id` bigint(20) unsigned NOT NULL,
`pv_date` datetime NOT NULL default '0000-00-00 00:00:00',
`pv_ip` varchar(200) NOT NULL,
PRIMARY KEY (`pv_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=235 ;
INSERT INTO `p_votes` (`pv_id`, `u_id`, `p_id`, `pv_date`, `pv_ip`) VALUES
(232, 237, 255, '2010-12-16 08:57:43', '85.146.204.228'),
(233, 237, 259, '2010-12-16 09:10:27', '85.146.204.228'),
(234, 237, 254, '2010-12-16 09:24:22', '85.146.204.228');
Any suggestions?
Given the small data set, instead of using a cross join, you could use an INNER JOIN
for your first query:
select *
from photos p
inner join p_votes v on (p.p_id = v.p_id and v.u_id = 237);
and you would easily find the complement you are looking for with a LEFT JOIN
:
select *
from photos p
left join p_votes v on (p.p_id = v.p_id and v.u_id = 237)
where v.p_id is null;
I try to avoid subqueries in simple statements like this when possible (sorry to @FatherStorm).
select * from photos where p_id not in (select distinct p_id from p_votes where u_id=237)
Change '!=' to '=' in 'p_votes.p_id = photos.p_id' in your "not equal" query.
SELECT * FROM photos, p_votes WHERE p_votes.p_id = photos.p_id AND p_votes.u_id != 237
精彩评论