表1 -> 访问控制
1) user_uuid
2) product_id
3) facebook_uuid
表2 -> 产品表
1) product_id
2) product_name
3) status
4) visibility
5) result
6) user_uuid
表3 -> 出价表
1) product_id
2) user_uuid
3) options
示例数据
产品表:-
ID - Name - status - visibility - result - user_uuid
1 - T1 - 1 - 1 - 0 - 1
2 - T2 - 1 - 1 - 0 - 1
3 - T3 - 0 - 0 - 1 - 1
访问控制
user_uuid - product_id - facebook_uuid
1 - 1 - 123
1 - 1 - 456
1 - 1 - 789
1 - 2 - 123
1 - 2 - 456
1 - 2 - 789
1 - 3 - 123
1 - 3 - 456
1 - 3 - 789
出价表:- 最初此表不会有任何数据,因为一旦任何用户对产品出价,则只会输入此数据。
product_id - user_uuid - options
1 - 2 - 123.35
用户表:-
user_uuid - facebook_uuid
1 - 007
2 - 123
3 - 456
4 - 789
解释:-
用户 1 创建了 3 个产品并分享给其他 facebook 用户。为了管理共享,我们创建了访问控制表,我们在其中跟踪用户 1 与其他用户共享产品的记录。您可以在访问控制表中查看。
现在可以与他人共享产品,因此用户可以在屏幕上看到与他们共享的产品。
我们在屏幕上有 2 个选项卡 -> 共享和出价。
Shared -> 在这里,用户将看到与他们共享的产品,但他们尚未对该产品出价。一旦用户对任何产品出价,我们就会在上面显示的出价表中进行输入。
出价 -> 在这里用户将看到他们出价的产品。
我们的查询:-
SELECT *
FROM PRODUCT TABLE
WHERE product_id IN
( SELECT product_id
from ACCESS TABLE
where facebook_uuid ='123'
and product_id not in
( Select product_id from BID TABLE)
)
or product_id IN
( SELECT product_id
from ACCESS TABLE
where user_uuid IN
( select user_uuid
from USER TABLE
where facebook_uuid = '123'
)
and product_id not in
( Select product_id from BID TABLE )
)
AND result = 0
AND status = 1
AND visibility = 1
这应该返回产品 2,因为用户 2 已经对产品 1 进行了投标,并且产品 3 的状态和可见性标准不匹配。但是上面的查询给了我 PRODUCT 2 和 3 都是错误的。
我们正在使用3个表来获取数据并寻求专家的帮助指导。
注意 - 我们必须检查 2 件重要的事情,我们在查询中无法做到的是,用户不得出价并由产品创建者与用户共享。
最佳答案
第一个子查询选择与给定用户共享的产品,第二个子查询选择给定用户已经出价的产品。因此查询选择在
第一个列表(由第一个子查询选择)但不在
第二个列表(由第二个子查询选择)中的产品。
select *
from product_table
where
result=0 and
status=1 and
visibility=1 and
product_id in (
select product_id
from access_table
where facebook_uuid='123'
) and
product_id not in (
select product_id
from
bid_table join
user_table on user_table.user_uuid=bid_table.user_uuid
where facebook_uuid='123'
)
关于使用3个表的Mysql查询返回错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499731/