选择内连接

时间:2015-07-10 00:58:30

标签: php sql select

我需要从两个表中选择一些数据, 请帮我使用内连接进行此选择。 selction2中的玩家不得参与选择1 ... 首先选择:

$rs = "SELECT * 
        FROM `player` 
        WHERE `status`=1 AND `credit`>=1 AND `username` NOT LIKE '$user' 
        ORDER BY ls ASC,credit DESC 
        LIMIT 0 ,10;

第二:这个玩家必须从选择结果中删除

$rs2 = "SELECT * 
        FROM `ip_log` 
        WHERE `playerid`='$ui' AND `win`='1' AND `date`='$date' ";`

2 个答案:

答案 0 :(得分:0)

您可以使用{scope:'email'}

这显示每个人都没有选择1的日志消息。

LEFT JOIN

这显示了前10名玩家数据,但选择2中包含日志消息的人数除外

SELECT l.*
FROM ip_log AS l
LEFT JOIN 
    (SELECT username
     FROM player
     WHERE status = 1 AND credit >= 1 AND username NOT LIKE '$user'
     ORDER BY ls ASC, credit DESC 
     LIMIT 10) AS p
ON l.player = p.username
WHERE win = 1 and date = '$date'
AND p.username IS NULL

在这两种情况下,使用SELECT p.* FROM player AS p LEFT JOIN ip_log AS l ON l.player = p.username AND l.win = 1 AND l.date = '$date' WHERE p.status = 1 AND p.credit >= 1 AND p.username NOT LIKE '$user' AND l.player IS NULL ORDER BY p.ls ASC, p.credit DESC LIMIT 10 测试第二个表中的列使得它仅返回第一个表中第二个表中没有匹配项的行。参见

Return row only if value doesn't exist

答案 1 :(得分:0)

您可以使用LEFT JOIN

进行操作
SELECT player.*,ip_log.* FROM `player` LEFT JOIN `ip_log` ON player.id!=ip_log.playerid GROUP BY player.id