1

我的 select 语句有问题,当运行 title_two 和 author_two 时,下一个标题值没有正确填充。相反,它们打印出与 b.title 和 b.author 相同的内容。我尝试将它们声明为

 `B.title AS title_two`.   

  SELECT R.bookone, B.title, B.author,
     R.booktwo, B.title title_two, B.author author_two, 
    R.relation, R.relationlike, R.relationdislike 
    FROM relationships R 
    INNER JOIN books B ON R.bookone = B.bookid;

有两张桌子

具有以下内容的关系-relationshipid、bookone、booktwo、relation、relationlike、relationdislike

然后是 bookid、title、author、publisher 的书

bookone 和 booktwo 是引用 bookid 的外键,我想要实现的是拥有它,这样当用户单击链接时,它会显示该书与其他书的所有关系

4

5 回答 5

0

Use Quotes while using alias

 SELECT R.bookone, B.title, B.author,
     R.booktwo, B.title as 'title_two', B.author as 'author_two', 
    R.relation, R.relationlike, R.relationdislike 
    FROM relationships R 
    INNER JOIN books B ON R.bookone = B.bookid;
于 2012-05-22T07:37:39.490 回答
0

You need to join on books one more time. Something like:

  SELECT R.bookone, B1.title, B1.author,
    R.booktwo, B2.title title_two, B2.author author_two, 
    R.relation, R.relationlike, R.relationdislike 
    FROM relationships R 
    INNER JOIN books B1 ON R.bookone = B1.bookid
    INNER JOIN books B2 ON R.booktwo = B2.bookid
于 2012-05-22T07:38:01.203 回答
0

他们正在做他们应该做的事情。author_two 应与 B.author 相同,title_two 应与 B.title 相同。他们只是别名。

此外,从一个表中两次请求同一列不会得到表中的下一个结果——它只会返回同一列两次。

于 2012-05-22T07:39:08.853 回答
0

如果别名在您的查询中不起作用,请检查您是否包含

useOldAliasMetadataBehavior=true

在 JDBC 连接字符串中。

https://bugs.mysql.com/bug.php?id=33966

于 2019-08-09T02:11:39.570 回答
-1

The correct format is: ColName as NewColName

SELECT R.bookone as `BookOne`
  , ...
FROM relationships R 
INNER JOIN books B ON R.bookone = B.bookid;

or simply ( without AS)

SELECT R.bookone `BookOne`
  , ...
FROM relationships R 
INNER JOIN books B ON R.bookone = B.bookid;
于 2012-05-22T07:37:40.037 回答