Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数据库连接问题_Java_Mysql_Database - Fatal编程技术网

Java 数据库连接问题

Java 数据库连接问题,java,mysql,database,Java,Mysql,Database,数据库中有两个表 Book{ isbn integer, //primary key title char(50), publisher char(30), year integer } Author{ isbn integer, //foreign key name String, rank integer, // indicate whether it is author1, author2, author3,...,authorn } 以这种方式编写一个sql语句到输出book

数据库中有两个表

Book{
isbn integer, //primary key
title char(50),
publisher char(30),
year integer
}

Author{
isbn integer,  //foreign key 
name String,
rank integer,  // indicate whether it is author1, author2, author3,...,authorn
}
以这种方式编写一个sql语句到输出book 标题 作者1 作者2 出版商,年

逻辑是根据isbn从Author表中选择两条记录,然后合并到Book中的记录并输出。我想我们可以用MySQL来做吗

谢谢。

试试群舞


在我的解决方案中有两个子查询:选择Book.title,从Author中选择Author.name,其中Author.isbn=Book.isbn和Author.rank='1'作为author1,从Author.isbn=Book.isbn和Author.rank='2'作为author2从Book中选择Author.name


有没有办法提高绩效?使用、连接、过程或其他方式?谢谢

author1 author2可以是一列吗?author3,…,authorn呢?你不需要这些吗?我们应该回答什么是明确的大学家庭作业问题吗?选择b.title,group_concata.name order by a.rank limit 2作为书b的作者加入作者a on b.isbn=a.isbn order by b.title。仍然需要它拿出Author1和Author2来生成Author1 Author2。AuthorN作为列标题,您可以使用本文所示的过程生成Author1 Author2的另一种方法。AuthorN作为列标题将首先查询作者的数量,然后在运行时构造所需的SQL语句。对,我更喜欢第二种解决方案,但如果我写。。。组_concata.name按a.rank限制排序2。。。我总是在限制2附近出现一个syntext错误。看起来我无法在组内添加限制2。组内的限制不受支持。它作为功能请求提交了两次。和
SELECT b.title, group_concat(a.name separator ',')
FROM book b join author a on b.isbn = a.isbn
group by b.title