开始切题了,不过还是有问题,问题是第二步第一步(查出所有有特价的记录): select A.*, B.price from A,B where A.member=B.mbember;第二步(查出其余剩下的记录): select A.*,B.price from A,B where B.member='' and (...????)
(不发意思,纠正): 第一步(查出所有有特价的记录): select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product;
我想分2步应该是肯定的。 方法1: (select A.*, B.price from A inner join B on A.product=B.product where A.member=B.member)full join (select A.*, B.price from A inner join B on A.product=B.product where B.member='') 2:就是你上面的,需要多录入一些数据测试一下 第一步(查出所有有特价的记录): select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product; 我觉得可能容易取迪卡尔乘积,不是很好的方法; 第二步(查出其余剩下的记录): select A.*,B.price from A,B where B.member='' and A.product=B.product; 信手涂鸦,多多指正。
用联合查询: 查出所有特价的记录如下: select a.* ,b.price from a ,b where a.member=b.member and a.prouduct=b.prouduct; 查出所有非特价记录: select a.* ,b.price from a ,b where b.member='' and a.prouduct=b.prouduct;
很遗憾,上面第二步还是不行的,因为你可能遗忘了一点,B表中是有一个默认价的,而它的member都是空的,上面的结果将会是这样: 命令: select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product; 结果: 会员甲,20ml装飘柔洗发水,其他信息, 5.00 命令: select A.*,B.price from A,B where B.member='' and A.product=B.product; 结果: 会员甲,20ml装飘柔洗发水,其他信息,20.00 会员甲,40ml装飘柔洗发水,其他信息,40.00 会员乙,20ml装飘柔洗发水,其他信息,20.00 会员乙,40ml装飘柔洗发水,其他信息,40.00看出有什么区别了吗? (问题是怎么去掉已经查出来的记录)
是我的问题,设计表的时候没加上id,或者说表达的时候把id的值也省去了,没有id写这个sql是有点麻烦,sorry!调整之后的结构是 A(id,member,product,other) B(id,member,product,price)我没办法用一条sql命令解决掉它(因为感觉mysql限制挺多的,其他数据库的解决方法参见这里:http://community.csdn.net/Expert/topic/3091/3091239.xml?temp=.2712061)非常感谢大家的帮助给了我很多帮助和启发,谢谢!!! (附最后的测试代码) <?php $sql="select A.*,B.price from A,B where A.member=B.member and A.product=B.product;"; $result=mysql_query($sql); echo $sql."<br>".$result."<br>"; $tem="0"; while ($rs=mysql_fetch_object($result)) { $tem=$tem.",".$rs->id; echo $rs->id." ".$rs->member." ".$rs->product." ".$rs->other." ".$rs->price."<br>"; }$sql="select A.*,B.price from A,B where B.member='' and B.product=A.product and A.id not in (".$tem.");"; $result=mysql_query($sql); echo $sql."<br>".$result."<br>"; while ($rs=mysql_fetch_object($result)) { echo $rs->id." ".$rs->member." ".$rs->product." ".$rs->other." ".$rs->price."<br>"; } ?>
说明一下,开始由于估计不足我说需要分两步走。其实应该有三步:CREATE TEMPORARY TABLE tempA SELECT A.*,B.price FROM A, B WHERE A.member = B.member and A.product = B.productCREATE TEMPORARY TABLE tempB select A.*,B.price from A,B where A.product = B.product and B.member = ''select tempB.member, tempB.product, ifnull(tempA.price,tempB.price) from tempB left join tempA on tempA.member = tempB.member and tempA.product = tempB.product 先产生两个临时表,然后连接两表计算出结果 虽然我给出的不是最佳方案,但也可知此类的查询难度系数较高 从而也可以推想出为什么作为轻型数据库的mysql在4.1以前不支持子查询的原因
A(member,product,other)
B(member,product,price)A表记录:会员甲,20ml装飘柔洗发水,其他信息
会员甲,40ml装飘柔洗发水,其他信息会员乙,20ml装飘柔洗发水,其他信息
会员乙,40ml装飘柔洗发水,其他信息B表记录:
,20ml装飘柔洗发水,20.00
,40ml装飘柔洗发水,40.00
甲 ,20ml装飘柔洗发水,5.00那么希望查出来的最后结果是:会员甲,20ml装飘柔洗发水,其他信息, 5.00
会员甲,40ml装飘柔洗发水,其他信息,40.00
会员乙,20ml装飘柔洗发水,其他信息,20.00
会员乙,40ml装飘柔洗发水,其他信息,40.00
select A.*, B.price from A,B where A.member=B.mbember;第二步(查出其余剩下的记录):
select A.*,B.price from A,B where B.member='' and (...????)
第一步(查出所有有特价的记录):
select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product;
方法1:
(select A.*, B.price from A inner join B on A.product=B.product where A.member=B.member)full join (select A.*, B.price from A inner join B on A.product=B.product where B.member='')
2:就是你上面的,需要多录入一些数据测试一下
第一步(查出所有有特价的记录):
select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product;
我觉得可能容易取迪卡尔乘积,不是很好的方法;
第二步(查出其余剩下的记录):
select A.*,B.price from A,B where B.member='' and A.product=B.product;
信手涂鸦,多多指正。
查出所有特价的记录如下:
select a.* ,b.price from a ,b where a.member=b.member and a.prouduct=b.prouduct;
查出所有非特价记录:
select a.* ,b.price from a ,b where b.member='' and a.prouduct=b.prouduct;
命令:
select A.*, B.price from A,B where A.member=B.mbember and A.product=B.product;
结果:
会员甲,20ml装飘柔洗发水,其他信息, 5.00
命令:
select A.*,B.price from A,B where B.member='' and A.product=B.product;
结果:
会员甲,20ml装飘柔洗发水,其他信息,20.00
会员甲,40ml装飘柔洗发水,其他信息,40.00
会员乙,20ml装飘柔洗发水,其他信息,20.00
会员乙,40ml装飘柔洗发水,其他信息,40.00看出有什么区别了吗?
(问题是怎么去掉已经查出来的记录)
会员甲,20ml装飘柔洗发水,其他信息, 5.00
会员甲,40ml装飘柔洗发水,其他信息,40.00
会员乙,20ml装飘柔洗发水,其他信息,20.00
会员乙,40ml装飘柔洗发水,其他信息,40.00由于mysql不支持子查询,要怎么在上面第二步查出的结果里面去掉第一步已经查出来的结果呢?用php控制?也就是说还要第三步?
A(id,member,product,other)
B(id,member,product,price)我没办法用一条sql命令解决掉它(因为感觉mysql限制挺多的,其他数据库的解决方法参见这里:http://community.csdn.net/Expert/topic/3091/3091239.xml?temp=.2712061)非常感谢大家的帮助给了我很多帮助和启发,谢谢!!!
(附最后的测试代码)
<?php
$sql="select A.*,B.price from A,B where A.member=B.member and A.product=B.product;";
$result=mysql_query($sql);
echo $sql."<br>".$result."<br>";
$tem="0";
while ($rs=mysql_fetch_object($result))
{
$tem=$tem.",".$rs->id;
echo $rs->id." ".$rs->member." ".$rs->product." ".$rs->other." ".$rs->price."<br>";
}$sql="select A.*,B.price from A,B where B.member='' and B.product=A.product and A.id not in (".$tem.");";
$result=mysql_query($sql);
echo $sql."<br>".$result."<br>";
while ($rs=mysql_fetch_object($result))
{
echo $rs->id." ".$rs->member." ".$rs->product." ".$rs->other." ".$rs->price."<br>";
}
?>
SELECT A.*,B.price
FROM A, B
WHERE A.member = B.member and A.product = B.productCREATE TEMPORARY TABLE tempB
select A.*,B.price
from A,B
where A.product = B.product and B.member = ''select tempB.member, tempB.product, ifnull(tempA.price,tempB.price)
from tempB left join tempA on tempA.member = tempB.member and tempA.product = tempB.product 先产生两个临时表,然后连接两表计算出结果
虽然我给出的不是最佳方案,但也可知此类的查询难度系数较高
从而也可以推想出为什么作为轻型数据库的mysql在4.1以前不支持子查询的原因