packages
表将在3个案例中加入记录。
优先级:
如果
dalel
有packageid
。如果会员资格表包含此dalel
packageid
的记录。- 醇>
以上2个都没有获得默认包
packageid
我的查询工作正常,但如果它会慢或对服务器内存有影响,我会徘徊这是最好的方法吗?
加入的一部分:
LEFT JOIN packages AS packages ON (CASE
WHEN dalel.fixedPackageid > 0
THEN packages.packageid = dalel.fixedPackageid
ELSE
( CASE
WHEN membership.packageid > 0
THEN packages.packageid = membership.packageid
ELSE packages.isDefualt = 1
END)
END)
整个查询:
SELECT packages.*,
category.category_title , user.first_name , user.last_name, user.email , user_group.title AS group_title,
dalel.*,
round( (((acos(sin((".$latitude."*pi()/180)) *
sin((dalel.latitude*pi()/180))+cos((".$latitude."*pi()/180)) *
cos((dalel.latitude*pi()/180)) * cos(((".$longitude."- dalel.longitude)*
pi()/180))))*180/pi())*60*1.1515
) * 1.609344 , 1 ) AS distance ,
province.province_title
" . ( $logdUserId > 0 ? ", rating.rateid AS rated " : "" ) . "
" . ( $logdUserId > 0 ? ", favorite.fid AS favorite " : "" ) . "
FROM dalel AS dalel
LEFT JOIN province AS province ON ( province.provinceid = dalel.stateOrProvince )
LEFT JOIN category AS category ON (category.category_id = dalel.category_id)
LEFT JOIN user AS user ON (user.userid = dalel.userid)
LEFT JOIN user_group AS user_group ON (user.groupid = user_group.groupid)
LEFT JOIN membership AS membership ON (membership.dalelid = dalel.dalelid AND expireDate > '" . $timeNow . "' )
LEFT JOIN packages AS packages ON (CASE
WHEN dalel.fixedPackageid > 0
THEN packages.packageid = dalel.fixedPackageid
ELSE
( CASE
WHEN membership.packageid > 0
THEN packages.packageid = membership.packageid
ELSE packages.isDefualt = 1
END)
END)
" . ($logdUserId> 0 ? "LEFT JOIN rating AS rating ON ( rating.userid = '" .$logdUserId. "' AND dalel.dalelid = rating.dalelid ) " : "" ) . "
" . ($logdUserId> 0 ? "LEFT JOIN favorite AS favorite ON ( favorite.userid = '" .$logdUserId. "' AND dalel.dalelid = favorite.dalelid ) " : "" ) . "
$where
GROUP BY dalel.dalelid
$having
ORDER BY $orderBy
这是连接表条件的最佳方式,或者有更好的方法 查询?