简体   繁体   English

MySQL SELECT的结果来自1个表,但排除结果取决于另一个表?

[英]MySQL SELECT results from 1 table, but exclude results depending on another table?

What SQL query would I have to use if I want to get the results from a table 'messages' but exclude rows that have the value in 'messages_view' where field messages.message=messages_view.id AND messages.deleted=1 AND messages_view.user=$somephpvariable 如果我想从表“ messages”中获取结果,但要排除具有“ messages_view”中的值的行,则其中必须使用哪种SQL查询,其中fields.message = messages_view.id AND messages.deleted = 1 AND messages_view。用户= $ somephp变量

In more laymen terms, I have a messages table with each message denoted by an 'id' as well as a messages_view table connected with a 'message' field. 用更多的外行术语来说,我有一个消息表,每个消息都由一个“ id”表示,还有一个message_view表与一个“ message”字段连接。 I want to get the rows in message that are not deleted (comes from messages_view ) for a specific 'user'. 我想为特定的“用户”获取未删除的消息行(来自messages_view )。 'deleted'=1 when the message is deleted. 删除邮件时,“已删除” = 1。

Here is my current SQL Query that just gets the values out of : 这是我当前的SQL查询,仅从中获取值:

SELECT * FROM messages WHERE ((m_to=$user_id) 
    OR (m_to=0 AND (m_to_state='' OR m_to_state='$state') 
    AND (m_to_city='' OR m_to_city='$city')))

Here is the layout of my tables: 这是我的桌子的布局:

table: messages
----------------------------
id (INT) (auto increment)
m_from (INT)                     <-- Represents a user id (0 = site admin)
m_to (INT)                       <-- Represents a user id (0 = all users)
m_to_state (VARCHAR)
m_to_city (VARCHAR)

table: messages_view
----------------------------
message (INT)                    <-- Corresponds to messages.id above
user (INT)                       <-- Represents a user id
deleted (INT)                    <-- 1 = deleted

I really think it's as simple as this: 我真的认为这很简单:

SELECT * FROM messages WHERE ((m_to=$user_id) 
    OR (m_to=0 AND (m_to_state='' OR m_to_state='$state') 
    AND (m_to_city='' OR m_to_city='$city')))
AND NOT EXISTS (
    SELECT *
    FROM messages_view
    WHERE messages.message = messages_view.id
        AND messages.deleted = 1 
        AND messages_view.user = $somephpvariable
)
Select ...
From Messages M
Where M.deleted = 0
    And Not Exists  (
                    Select 1
                    From Messages_View MV1
                    Where MV1.message = messages_view.Id
                        And MV1.user = $somephpvariable
                    )

There is a contradiction from your first paragraph and your second paragraph as to what you want with respect to the deleted flag. 您的第一段和第二段在关于已删除标志的要求方面存在矛盾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从表中选择所有结果,具体取决于PHP和MySQL中的另一个表(关系数据库) - Cannot select all results from a table depending on another table (relational DB) in PHP and MySQL 如何根据mySQL中另一个表的ID省略选择结果? - How to omit select results based on IDs from another table in mySQL? PHP PDO MySQL根据另一个表的结果数组选择一个表的结果 - php PDO MySQL Select results of one table based on array of results from another table MySql嵌套从同一个表中选择结果 - MySql nested select from the same table with the results 从clumon“”中选择结果并加入另一个表 - select results from clumon “” and join another table 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql 根据另一个表的结果导入到mysql - Import into mysql based on results from another table 从mysql表中获取结果,同时从另一个表中获取结果 - get results from mysql table while matching results from another MYSQL:将SELECT结果中的多个结果插入表中 - MYSQL: Inserting multiple results from SELECT results into a table 从MySQL表中选择数据,其中来自另一个表的某些结果取决于条件(如果有) - Select data from a MySQL table where some results from another table depends on a condition, if there are any
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM