我的目的是通过在donation_status表中给出status ='yes'的条件来打印邮件ID,地址和宠物类型,pet_details的id =捐赠者的pet_id和收件人的pet_id以及成员的ID =捐赠者广告的member_id member_id =收件人。
简单来说,我想显示捐赠者和接受者的详细信息以及捐赠宠物的类型
图1:表格 - 成员和捐赠者 图2:表格 - 收件人,donation_status,pet_details
我试过的查询是
select members.mail_id,members.address,pet_details.type from members,pet_details,donor,recipient,donation_status where members.id in (select pet_id from members,pet_details,donor,recipient,donation_status where status = 'yes' in (select member_id from donor,members,pet_details,donor,recipient where donor.member_id = recipient.member_id in (select pet_id from members,pet_details,donor,recipient where pet_id.donation_status = id.pet_details)))
我得到的错误是,
ERROR 1066(42000):不是唯一的表/别名:'donor'
答案 0 :(得分:1)
执行此查询的正确方法是使用JOIN
:
SELECT m1.mail_id AS donor, m1.address AS `donor address`, m2.mail_id AS recipient, m2.address AS `recipient_address`, pet_details.type as `pet type`
FROM members m1
JOIN donor ON donor.member_id = m1.id
JOIN recipient ON recipient.pet_id = donor.pet_id
JOIN donation_status ON donation_status.pet_id = donor.pet_id
JOIN members m2 ON m2.id = recipient.member_id
JOIN pet_details ON pet_details.id = donor.pet_id
WHERE donation_status.status = 'yes'
输出(似乎是一名成员向自己捐赠了一只狗):
donor donor address recipient recipient_address pet type
mn.sha222@gmail.com Gandhinagar Coimbatore mn.sha222@gmail.com Gandhinagar Coimbatore dog