2

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

我正在那里建立一个站点,您可以对帖子(图像)进行投票,对于此页面,您应该只看到没有任何投票的帖子。

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

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;
4

3 回答 3

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)
于 2013-09-15T14:47:55.397 回答
0

获取 Table1 中不在 Table2 中的行并排序随机数并获取第一条记录。

使用内部查询

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()always 也很好。

于 2013-09-15T14:47:53.197 回答
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)
于 2013-09-15T14:48:15.853 回答