连接的子查询(在/存在)
Posted
技术标签:
【中文标题】连接的子查询(在/存在)【英文标题】:Subqueries (in / exists) to Joins 【发布时间】:2015-04-02 05:25:33 【问题描述】:SELECT *, `o_cheque_request.member_id`, `o_cheque_request.wallet_id`
FROM `o_cheque_request`, `o_member`, o_memberinfo`
WHERE `o_cheque_request.member_id` = `o_member.member_id`
AND `o_member.member_id` = `o_memberinfo.member_id`
AND withdraw_date >='2012-07-21'
AND `o_cheque_request.member_id`
IN (SELECT `member_id` FROM `o_cheque_request` GROUP BY `member_id` HAVING SUM(gross_amount ) <=10000)
GROUP BY `o_cheque_request.withdraw_date`, `o_cheque_request.member_id` ORDER BY `request_id` DESC
这需要 29 秒左右的时间如何减少...使用连接...请大家帮帮我...o_cheque_requestrequest_id
bigint(20) 的表结构unsigned NOT NULL auto_increment,wallet_id
int(11) NOT NULL 默认 '0',member_id
int(10) unsigned NOT NULL 默认 '0',withdraw_date
date 默认 NULL, amount
int(10) unsigned NOT NULL default '0',gross_amount
float(10,2) unsigned NOT NULL default '0.00',admin_charge
float(10,2)无符号非空默认'0.00',tds
浮点(10,2)无符号非空默认'0.00',repurchase
浮点(10,2)无符号非空默认'0.00',net_amount
float(10,2) unsigned NOT NULL 默认为 '0.00',withdraw_type
varchar(50) 默认为 NULL,bank_name
varchar(50) 默认为 NULL,cheque_no
varchar(50) 默认 NULL,courier_name
varchar(50) 默认 NULL,tracking_no
varchar(50) 默认 NULL,cheque_date
日期默认 '0000-00-00',
主键 (request_id
),
键member_id
(member_id
)
) ENGINE=InnoDB 默认字符集=latin1 AUTO_INCREMENT=4738 ;
【问题讨论】:
29 秒获取多少条记录?并请提供您的表结构。 嘿,我在 o_cheque_request 表中有 4000 多个条目... 请以正确的方式提供表结构,并为您在查询中使用的三个表及其内部关系提供。 每个表都有共同的字段名称 member_id 来连接它们 【参考方案1】:试试这个
SELECT * FROM (
SELECT *, `o_cheque_request.member_id`, `o_cheque_request.wallet_id`
FROM `o_cheque_request`
JOIN `o_member` ON `o_cheque_request.member_id` = `o_member.member_id`
JOIN `o_memberinfo` ON `o_member.member_id` = `o_memberinfo.member_id`
WHERE withdraw_date >='2012-07-21'
GROUP BY `o_cheque_request.member_id` HAVING SUM(`o_cheque_request.gross_amount` ) <=10000
) AS T
GROUP BY `withdraw_date`, `member_id` ORDER BY `request_id` DESC
【讨论】:
感谢回复,但得到 error :: 您的 SQL 语法有错误;检查与您的 mysql 服务器版本相对应的手册,以在第 1 行的 '( SELECT *,o_cheque_request.member_id
, `o_cheque_request.wal' 附近使用正确的语法以上是关于连接的子查询(在/存在)的主要内容,如果未能解决你的问题,请参考以下文章