簡體   English   中英

通過將兩列連接在一起從MySQL中選擇?

[英]Select from MySQL by connecting two columns together?

為了讓您大致了解我在這里要實現的目標,我建立了一個非常簡單的郵件系統,用戶可以在其中“加注星標”傳入或傳出的郵件(以供日后閱讀或保存等)。 該系統非常簡單,並且與以下表格設計類似,請注意,我刪除了一些與此處無關的列:

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

從上面的示例中,如果我們獲得用戶ID為10已加星標的消息,則查詢將顯示Message ID的1, 3 您可能可以解決-如果僅分別查看“已接收”或“已發送”郵件,我可以輕松檢索已加星標的郵件。 雖然,這里的問題是我想為每個用戶在單個文件夾中顯示所有“已加星標”消息,並且顯然這需要確定哪個用戶已對該消息加星標,或者通過連接recip_account和recip_starred或sender_account&sender_starred和引用針對用戶ID?

有人可以在這里幫我嗎,目前我檢索結果的方式如下:

$sql2 = "SELECT * FROM `ap_mail_system` WHERE `recip_account` = '$user_id' OR `sender_account` = '$user_id' ORDER BY `sent_date` DESC LIMIT 0, 12"; 

您只需要在WHERE子句中使用一些布爾邏輯即可。 嘗試:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM