按另一个表中的列排序

g0czyy6m  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(404)

我需要一份数据库里所有书的清单。清单应包括:
出版商名称、书名、所有书籍的出版年份。
列表必须按出版商和标题的字母顺序排序。
我无法对两种情况的列表进行排序,只能对其中一种情况进行排序。
以下是表格:
发布者表

cursor.execute("DROP TABLE IF EXISTS `Publishers`;")
cursor.execute('''
CREATE TABLE `Publishers` (
  `PublisherID` INTEGER PRIMARY KEY AUTOINCREMENT,
  `Name` VARCHAR(45) NULL,
  `City` TEXT NULL,
  `Country` TEXT NULL
);

书桌

cursor.execute("DROP TABLE IF EXISTS `Books`;")
   cursor.execute('''
   CREATE TABLE `Books` (
  `BookID` INTEGER PRIMARY KEY AUTOINCREMENT,
  `Title` VARCHAR(45) NULL,
  `ISBN` VARCHAR(45) NULL,
  `ISBN13` TEXT NOT NULL CHECK (length(ISBN13)=13), 
  `PublisherID` INT NOT NULL,
  `Year_Published` VARCHAR(45) NULL,
  `Price` DOUBLE NULL,
  FOREIGN KEY (`PublisherID`) REFERENCES Publishers(PublisherID) ON DELETE CASCADE ON UPDATE CASCADE
);
''')

我可以写这篇文章,但不能对这两种情况进行排序

cursor = conn.cursor()
for row in cursor.execute('''SELECT Publishers.name,title,Year_Published 
                            FROM books
                            INNER JOIN publishers on publishers.PublisherID = books.PublisherID
                            ORDER BY
                            title ASC, name.publisher ASC;'''):
                          print(row)
cursor.close()
vojdkbi0

vojdkbi01#

您似乎在寻找一个连接:

select p.name, b.title, b.year_published
from books b
inner join publishers p on p.publisherID = b.publisherID
order by p.name, b.title
6bc51xsx

6bc51xsx2#

我写错了条件。答案似乎是以下感谢大家!1

cursor = conn.cursor()
for row in cursor.execute('''SELECT Publishers.name,title,Year_Published 
                            FROM books
                            INNER JOIN publishers on publishers.PublisherID = books.PublisherID
                            ORDER BY
                            publishers.name ASC, books.title ASC;'''):

                          print(row)
cursor.close()

相关问题