我需要根据投标数量来安排我网站中的项目列表(投标存储在另一个表中)。生成投标后,将在投标表中添加新的值行我必须获取每个项目的投标计数并根据计数安排项目。
在下面的代码中,我使用 count(bid.project_id) 来获取每个项目的投标计数值,但是当我使用此代码时,我无法获取所有投标的项目列表为 0。
SELECT DISTINCT p.*,(TO_DAYS(p.start_date) - TO_DAYS(NOW())) AS daydiff,date(DATE_ADD(p.start_date,INTERVAL p.expires DAY)) as end_date,count(bid.project_id) as bidcount FROM c6wz3_jblance_project p
LEFT JOIN c6wz3_jblance_user ju ON p.publisher_userid = ju.user_id
LEFT JOIN c6wz3_jblance_custom_field_value cv ON cv.projectid=p.id
LEFT JOIN c6wz3_jblance_bid as bid ON bid.project_id=p.id
WHERE (p.project_title LIKE '%%' OR ju.biz_name LIKE '%%' OR cv.value LIKE '%%' OR cv.valuetext LIKE '%%' OR p.description LIKE '%%') AND p.approved=1 AND '2013-04-26 12:18:03' > p.start_date
GROUP BY bid.project_id
ORDER BY bidcount ASC
这是用户表:
CREATE TABLE IF NOT EXISTS `c6wz3_jblance_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`biz_name` varchar(100) NOT NULL,
`status` text NOT NULL,
`status_access` int(11) NOT NULL DEFAULT '0',
`posted_on` datetime NOT NULL,
`picture` varchar(50) NOT NULL,
`thumb` text NOT NULL,
`invite` int(11) NOT NULL DEFAULT '0',
`params` text NOT NULL,
`latitude` float NOT NULL DEFAULT '255',
`longitude` float NOT NULL DEFAULT '255',
`ug_id` int(11) NOT NULL DEFAULT '0',
`search_email` tinyint(1) NOT NULL DEFAULT '1',
`notify` tinyint(1) NOT NULL DEFAULT '1',
`suspend` tinyint(1) NOT NULL DEFAULT '0',
`suspend_reason` varchar(100) NOT NULL,
`featured` tinyint(1) DEFAULT '0',
`rate` float DEFAULT NULL,
`id_category` varchar(50) NOT NULL,
`featured_expire` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
这是对投标表的查询:
CREATE TABLE IF NOT EXISTS `c6wz3_jblance_bid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`project_id` int(11) NOT NULL DEFAULT '0',
`amount` float NOT NULL,
`delivery` int(11) NOT NULL DEFAULT '0',
`bid_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`details` mediumtext NOT NULL,
`outbid` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(20) NOT NULL,
`milestone_perc` int(11) NOT NULL DEFAULT '0',
`attachment` varchar(255) DEFAULT NULL,
`is_nda_signed` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ibid` (`project_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
此查询用于项目表:
CREATE TABLE IF NOT EXISTS `c6wz3_jblance_project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_title` varchar(255) NOT NULL,
`id_category` varchar(50) NOT NULL,
`start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`create_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`expires` int(11) NOT NULL DEFAULT '0',
`assigned_userid` int(11) NOT NULL DEFAULT '0',
`publisher_userid` int(11) NOT NULL DEFAULT '0',
`status` varchar(32) NOT NULL,
`budgetmin` float NOT NULL DEFAULT '0',
`budgetmax` float NOT NULL DEFAULT '0',
`description` text NOT NULL,
`is_featured` tinyint(1) DEFAULT '0',
`profit` float NOT NULL DEFAULT '0',
`paid_amt` float NOT NULL DEFAULT '0',
`paid_status` varchar(32) NOT NULL,
`approved` int(1) NOT NULL DEFAULT '1',
`profit_additional` float NOT NULL DEFAULT '0',
`is_urgent` tinyint(1) NOT NULL DEFAULT '0',
`is_private` tinyint(1) NOT NULL DEFAULT '0',
`is_sealed` tinyint(1) NOT NULL DEFAULT '0',
`is_nda` tinyint(1) NOT NULL DEFAULT '0',
`metakey` text,
`metadesc` text,
`buyer_commission` float NOT NULL DEFAULT '0',
`lancer_commission` float NOT NULL DEFAULT '0',
`accept_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;