好的,所以我在Mysql服务器上有一个卖家评论的表:
SellerReview (ID, User_ID, Time, Review, Seller_id)
和另一个表Seller_id
与名称Users(ID,name)相关联的表。
现在我必须进行查询,以便选择元组(V1,V2),其中V2的最大评论严格小于V1的最小评论。
我拥有的是:
SELECT U1.name AS V1, U2.naam AS V2
FROM Users AS U1, Users AS U2, SellerReview AS S1, SellerReview AS S2
WHERE U1.id = S1.seller_id
AND G2.id = V2.seller_id
AND ((SELECT MAX(S.Review)
FROM SellerReview AS S
WHERE S.Seller_id = U1.id) > (SELECT MIN(S.review)
FROM SellerReview AS S
WHERE S.Seller_id = G2.id))
但我找不到合适的桌子。有谁知道这个问题或任何可以帮助我的人?谢谢!
答案 0 :(得分:2)
要让所有评论者都不重复,请加入having
。从那里,您可以使用SELECT U1.name AS V1, U2.name AS V2
FROM Users AS U1
JOIN SellerReview AS S1 on U1.id = S1.seller_id
JOIN Users AS U2 ON U2.id < U1.id
JOIN SellerReview S2 on U2.id = S2.seller_id
GROUP BY U1.name, U2.name
HAVING MAX(S2.review) < MIN(S1.review)
确保第一个用户的最大审核小于另一个用户的最小审核。
Users.name
此查询假定Users.id
是唯一的。如果不是,则应该按// $source = file_get_contents($file);
$source = 'Array
(
[f_name] => YOKICHI
[l_name] => KOSHIZAWA
[name] => YOKICHI KOSHIZAWA
[street_address] => 7164 fake
[city] => CANTON
[state] => MI
[zip] => 48187
[country] => United States
[phone] => 734-354-6599
[email] => ykoshizawxxa@att.net
[discount_percent] => 0.00
[discount_amount] => 0
[discount_items] =>
[shipping_region] => USA
[shipping_type] =>
[shipping_priority] =>
[shipping_options] =>
[shipping_total] => 0
)';
// convert input to array
$arr = explode(PHP_EOL, $source);
// clean top and bottom of the array
array_shift($arr);
array_shift($arr);
array_pop($arr);
// init final array
$final = array();
foreach($arr as $v)
{
$o = array();
$v = preg_match_all('#\[([a-zA-Z0-9 _.-]+)\] => (.*)#', $v, $o);
$key = $o[1][0];
$val = (isset($o[2][0])) ? $o[2][0] : '';
// fill final
$final[$key] = $val;
}
// init the filtered array
$sort = array();
// keys you want to have in
$keys = array('f_name','l_name','street_address','city','state','zip','phone','country');
foreach($final as $k => $v)
{
if(in_array($k, $keys))
{
$sort[$k] = $v;
}
}
var_dump($sort);
进行分组,然后加入“用户”表以获取名称。