从Table1中选择一个随机行,其中表2中不存在id

时间:2013-09-15 14:39:40

标签: php mysql sql

我正在尝试从Table1中选择一(1)随机行,其中Table2中的Data_ID不存在于表2中。

我正在那里建立一个网站,你可以对帖子(图片)进行投票,对于这个页面你只能看到没有任何投票的帖子。

这是我的两个表的简单结构:

CREATE TABLE IF NOT EXISTS `Table1` (
  `Id` int(10) NOT NULL AUTO_INCREMENT,
  `Data_ID` varchar(10) NOT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Id` (`Id`,`Data_ID`),
  UNIQUE KEY `Data_ID` (`Data_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4389 ;


CREATE TABLE IF NOT EXISTS `Table2` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Data_ID` varchar(10) NOT NULL,
  `IP` varchar(20) NOT NULL,
  `Vote_ID` int(4) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2314 ;

谢谢!

编辑:

这是我的尝试,但我并没有随机发帖。

SELECT newTable.*
FROM (    
      SELECT Table1.Id, Table1.Data_ID FROM Table1
      LEFT OUTER JOIN Table2
      ON Table1.Data_ID = Table2.Data_ID
      WHERE Table2.Data_ID IS null
     ) as newTable
WHERE newTable.Id >= ROUND( RAND() * (SELECT MAX(Id) FROM Table1)) LIMIT 1;

3 个答案:

答案 0 :(得分:2)

你太复杂了:使用EXISTS的子查询就足够了。

SELECT
    Table1.Id,
    Table1.Data_ID
FROM
    Table1
WHERE
    NOT EXISTS(
        SELECT * FROM Table2 WHERE Table2.Data_ID = Table1.Data_ID
    )
ORDER BY
    RAND()
LIMIT
    1;

以下是DESCRIBE结果:

******************** 1. row *********************
           id: 1
  select_type: PRIMARY
        table: Table1
         type: index
possible_keys: 
          key: Data_ID
      key_len: 32
          ref: 
         rows: 1
        Extra: Using where; Using index; Using temporary; Using filesort
******************** 2. row *********************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: Table2
         type: ALL
possible_keys: 
          key:
      key_len: 
          ref: 
         rows: 1
        Extra: Using where

UPDv1:如果您喜欢JOIN时尚,还有另一种快捷方式:

SELECT
    Table1.Id,
    Table1.Data_ID
FROM
    Table1 LEFT OUTER JOIN Table2 USING(Data_ID)
WHERE
    Table2.Id IS NULL
ORDER BY
    RAND()
LIMIT
    1;

DESCRIBE结果:

******************** 1. row *********************
           id: 1
  select_type: SIMPLE
        table: Table1
         type: index
possible_keys: 
          key: Data_ID
      key_len: 32
          ref: 
         rows: 1
        Extra: Using index; Using temporary; Using filesort
******************** 2. row *********************
           id: 1
  select_type: SIMPLE
        table: Table2
         type: ALL
possible_keys: 
          key: 
      key_len: 
          ref: 
         rows: 1
        Extra: Using where; Not exists; Using join buffer (Block Nested Loop)

答案 1 :(得分:0)

获取表2中没有的Table1行并输入随机数并获取第一条记录。

使用内部查询

SELECT *
FROM Table1 t1 
WHERE t1.Data_ID NOT IN ( SELECT t2.Data_ID FROM Table2 t2)
ORDER BY FLOOR(RAND()*MAX(Id)) 
LIMIT 1

或使用不存在

SELECT *
FROM Table1 t1 
WHERE NOT EXISTS ( SELECT t2.Data_ID FROM Table2 t2 WHERE t2.Data_ID = t1.Data_ID)
ORDER BY FLOOR(RAND()*MAX(t1.Id)) 
LIMIT 1

或使用左连接

SELECT *
FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.Data_ID = t2.Data_ID
WHERE t2.ID IS NULL
ORDER BY FLOOR(RAND()*MAX(t1.Id)) 
LIMIT 1

如果您的表格中没有大量数据,您可以将FLOOR(RAND()*MAX(Id))替换为RAND()以获取随机数据。事实上,使用RAND()总是很好。

答案 2 :(得分:0)

SELECT * FROM table1 t1 
LEFT JOIN table2 t2 ON (t1.id=t2.id AND t1.data_id<>t2data_id) 
WHERE id=(SELECT id FROM table1 ORDER BY RAND() LIMIT 1)