简体   繁体   English

MySQL选择别名不起作用

[英]mysql select alias not working

Im having trouble with my select statement, when run title_two and author_two are not being filled properly by the next title value. 我在执行select语句时遇到麻烦,当运行title_two和author_two时,下一个标题值未正确填充。 Instead they print out the same as b.title and b.author. 相反,它们的打印内容与b.title和b.author相同。 I've tried declaring them as 我试过将它们声明为

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

there are two tables 有两个桌子

relationships which has the following - relationshipid, bookone, booktwo, relation, relationlike, relationdislike 具有以下关系-Relationshipid,bookone,booktwo,relation,relationlike,relationdislike

then books which is bookid, title, author, publisher 然后是书号,书名,作者,出版商的书

bookone and booktwo are foreign keys referencing bookid what im trying to acheive is have it so that when a user clicks a link itll come up with every relationship that book has with other books bookone和booktwo是引用bookid的外键,我试图达到的目的是拥有它,因此当用户单击链接时,它将显示该书与其他书的每种关系

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;

The correct format is: ColName as NewColName 正确的格式是: ColName作为NewColName

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

or simply ( without AS) 或简单地(不带AS)

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

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

They are doing what they're supposed to do. 他们正在做应该做的事情。 author_two should be the same as B.author, and title_two should be the same as B.title. author_two应与B.author相同,title_two应与B.title相同。 They are just aliases. 它们只是别名。

Also, asking for the same column twice from a single table won't get the next result in the table -- it just returns the same column twice. 同样,从单个表中两次请求同一列将不会获得表中的下一个结果-它只会返回同一列两次。

If aliasing does not work in your queries, please check if you have included 如果您的查询中无法使用别名,请检查是否包含

useOldAliasMetadataBehavior=true useOldAliasMetadataBehavior = true

in JDBC connection string. 在JDBC连接字符串中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM