1

我有两个要同时查询的表。这些表是上传和受众上传关联,所以事情就是这样,我的表中只有一个名为“ESL”的主题值,但是当我使用下面的 SQL 时,它给了我 6 条记录,它重复相同的数据 6 次。

SELECT uploads.*, audienceuploadassociation.*
FROM   uploads
  JOIN audienceuploadassociation
    ON uploads.upload_id = audienceuploadassociation.upload_id
 WHERE uploads.member_id = '1'
   AND uploads.member_school_id='1'
   AND subject = 'ESL'
   AND topic = 'Poetry'
LIMIT  20

这是上传的表结构:

`uploads` (
  `upload_id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` varchar(255) NOT NULL,
  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `upload_name` text NOT NULL,
  `upload_title` varchar(255) NOT NULL,
  `upload_type` varchar(100) NOT NULL,
  `subject` varchar(50) NOT NULL,
  `topic` longtext NOT NULL,
  `year` int(100) DEFAULT NULL,
  `keywords` varchar(255) NOT NULL,
  `description` longtext NOT NULL,
  `status` enum('Finished','Work in progress','Blank template') NOT NULL DEFAULT 'Finished',
  `test_name` varchar(255) DEFAULT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `member_display_name` varchar(255) NOT NULL,
  `member_school_id` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`upload_id`)
) 

对于观众上传协会:

`audienceuploadassociation` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `upload_id` bigint(20) NOT NULL,
  `audiencename` varchar(254) NOT NULL,
  PRIMARY KEY (`id`)
) 

任何帮助将不胜感激,在此先感谢。

4

2 回答 2

0

试试这个,使用 GROUP BY:

SELECT uploads.*, audienceuploadassociation.*
FROM   uploads
  JOIN audienceuploadassociation
    ON uploads.upload_id = audienceuploadassociation.upload_id
 WHERE uploads.member_id = '1'
   AND uploads.member_school_id='1'
   AND subject = 'ESL'
   AND topic = 'Poetry'
GROUP BY uploads.upload_id
LIMIT  20
于 2012-06-07T18:45:20.533 回答
0

我看不出这两个表之间有任何关系。

在您的uploads表定义中更改此

`member_school_id` int(11) NOT NULL DEFAULT 1

删除引号,member_school_id因为它是一个INT字段。

SELECT uploads.*, audienceuploadassociation.*
FROM   uploads
  JOIN audienceuploadassociation
    ON uploads.upload_id = audienceuploadassociation.upload_id
 WHERE uploads.member_id = '1'
   AND uploads.member_school_id=1
   AND uploads.subject = 'ESL'
   AND uploads.topic = 'Poetry'
LIMIT  20
于 2012-06-07T18:48:39.997 回答