[英]Select from MySQL by connecting two columns together?
To give you an idea of what I'm trying to achieve here, I've set up a very simple mail system where users can "Star" incoming or outgoing mail (To read later or save, etc...). 为了让您大致了解我在这里要实现的目标,我建立了一个非常简单的邮件系统,用户可以在其中“加注星标”传入或传出的邮件(以供日后阅读或保存等)。 The system is VERY simple and works with a similar table design to below, please note that I've removed some columns which aren't relevant here: 该系统非常简单,并且与以下表格设计类似,请注意,我删除了一些与此处无关的列:
Random Digit
User ID of Recipient
User ID of Sender
Message
If recipient has starred message (0 for no, 1 for yes)
If sender has starred the message (0 for no, 1 for yes)
mail_id | recip_account | sender_account | message | recip_starred | sender_starred
1 5 10 Hello 0 1
2 10 5 A Reply 0 1
3 10 20 Test 1 0
4 15 20 Message 1 1
From the examples above, if we are getting starred messages for user ID 10
, the query would show Message ID's 1, 3
. 从上面的示例中,如果我们获得用户ID为10
已加星标的消息,则查询将显示Message ID的1, 3
。 As you can probably work out - I can retrieve starred messages easily if viewing only Received or Sent messages separately. 您可能可以解决-如果仅分别查看“已接收”或“已发送”邮件,我可以轻松检索已加星标的邮件。 Although, the issue here is I would like to show all "Starred" messages in a single folder for each user, and obviously that requires working out which user has starred the message, or by connecting the recip_account & recip_starred OR sender_account & sender_starred and reference that against a user ID? 虽然,这里的问题是我想为每个用户在单个文件夹中显示所有“已加星标”消息,并且显然这需要确定哪个用户已对该消息加星标,或者通过连接recip_account和recip_starred或sender_account&sender_starred和引用针对用户ID?
Can anybody help me out here, the currently way I am retrieving results is as follows: 有人可以在这里帮我吗,目前我检索结果的方式如下:
$sql2 = "SELECT * FROM `ap_mail_system` WHERE `recip_account` = '$user_id' OR `sender_account` = '$user_id' ORDER BY `sent_date` DESC LIMIT 0, 12";
You just need a bit of boolean logic in your WHERE
clause. 您只需要在WHERE
子句中使用一些布尔逻辑即可。 Try: 尝试:
SELECT * FROM `ap_mail_system` WHERE (`recip_account` = '$user_id' AND `recip_starred` = 1) OR (`sender_account` = '$user_id' AND `sender_starred` = 1) ORDER BY `sent_date` DESC LIMIT 0, 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.