如果相关表上存在id,如何查询表不包含数据?

时间:2016-07-15 10:48:00

标签: php mysql

这是我的表格。

通知表

+----+----------------+--------+
| Id | NotificationId | Status |
+----+----------------+--------+
| 1  | 1              | Read   |
+----+----------------+--------+

UserNotification Table

NotificationId

如果UserNotification表中存在+----+-------------+ | Id | Subject | +----+-------------+ | 2 | Hello World | | 3 | Pls Help | +----+-------------+ ,我想查询通知表而不包含数据

我想要结果:

通知表

public static Color ColorFromGuid(UUID id)
{
    var values = id.ToByteArray().Select(b => (int)b);
    int red = values.Take(5).Sum() % 255;
    int green = values.Skip(5).Take(5).Sum() % 255;
    int blue = values.Skip(10).Take(5).Sum() % 255;

    Color color = Color.FromArgb(200, red, green, blue);
    return color;
}

8 个答案:

答案 0 :(得分:3)

使用NOT EXISTS

<强>查询

SELECT * FROM Notification n
WHERE NOT EXISTS(
    SELECT 1 FROM UserNotification u
    WHERE n.Id = u.NotificationId
);

sql fiddle demo

答案 1 :(得分:2)

您可以不在

中使用
select Id,Subject from notificationTable
where Id not in (select Id from userNotificationTable) ;

答案 2 :(得分:1)

您也可以使用JOIN

SELECT n.*
FROM Notification n
JOIN UserNotification un
ON n.Id <> un.NotificationId

SQLFiddle Demo

答案 3 :(得分:1)

你可以做点什么......

LEFT JOIN

<强> SQLFiddle

答案 4 :(得分:1)

您可以通过NULL SELECT N.Id, N.Subject FROM `Notification` N LEFT JOIN `UserNotification` U ON U.NotificationId = N.Id WHERE U.NotificationId IS NULL 检查来实现此目的:

{
    "response_code": 200,
    "error": false,
    "train_name": "KCG YPR EXP",
    "train_num": "17603",
    "pnr": "1234567890",
    "failure_rate": 19.346153846153847,
    "doj": "20-8-2015",
    "chart_prepared": "Y",
    "class": "SL",
    "total_passengers": 2,
    "train_start_date": {
        "month": 8,
        "year": 2015,
        "day": 20
    },
    "from_station": {
        "code": "KCG",
        "name": "KACHEGUDA"
    },
    "boarding_point": {
        "code": "KCG",
        "name": "KACHEGUDA"
    },
    "to_station": {
        "code": "YPR",
        "name": "YESVANTPUR JN"
    },
    "reservation_upto": {
        "code": "YPR",
        "name": "YESVANTPUR JN"
    },
    "passengers": [
        {
            "no": 1,
            "booking_status": "S7,58,GN",
            "current_status": "S7,58",
            "coach_position": 9
        },
        {
            "no": 2,
            "booking_status": "S7,59,GN",
            "current_status": "S7,59",
            "coach_position": 9
        }
    ]
}

答案 5 :(得分:0)

使用NOT IN

Select * from Notification where id NOT IN (SELECT id FROM UserNotification)

这个sql将有助于获取数据

答案 6 :(得分:0)

SELECT n.Id, n.Subject FROM Notification n
LEFT JOIN UserNotification nu on nu.NotificationId = n.id
WHERE nu.id IS NULL

答案 7 :(得分:0)

您想要返回用户通知中没有的通知

试试这个:

select Id,Subject
from Notification  
where 
id not in (
select distinct NotificationId  from UserNotification
)