-3

We have two tables.

1. information

title    body     name   

title1   body1   author1   
title2   body2   author1    
title1   body1  author2 
title1   body1   author3

2. interactions

name      favorited    user_favorited_by

author1      yes          user1 
author2      yes          user1 
author1      yes          user2 
author1      no          user3

The question is: who is for each user the favourite author or authors?

The query should give us the following answer based on the example:

user1    author1
user1    author2
user2    author1

Any help appreciated.

4

3 回答 3

1

这是一个有效的 JOIN:

SELECT DISTINCT user_favorited_by,   a.name
  FROM information a
  JOIN interactions b
  ON a.name = b.name
 WHERE  favorited = 'yes'

由于您只有“名称”可以加入,因此您需要 DISTINCT,或者您可以按所选字段分组以从输出中删除重复行。

这是一个演示: SQL Fiddle

于 2013-06-12T15:45:53.477 回答
0

我可以建议:

select user_favorited_by, author from information a
inner join interaction b on a.name = b.name 
where b.favorited = 'yes'
于 2013-06-12T15:35:45.250 回答
0

我认为以下应该有效

SELECT DISTINCT user_favorited_by,
                a.name
  FROM information a,
       interactions b
 WHERE a.name = b.name
   AND favorited = 'yes'
于 2013-06-12T15:43:21.070 回答