sql从某些条件获取表中的所有记录

时间:2015-04-06 05:33:38

标签: mysqli

我的表'书'有以下结构

CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) NOT NULL,
  `bookname` varchar(100) NOT NULL,
  `bookimage` varchar(100) NOT NULL,
  `author_id` int(11) DEFAULT '0',
  `publisher_id` int(11) DEFAULT '0',
  `published_date` varchar(10) DEFAULT '',
  `genre` varchar(20) NOT NULL,
  `description` varchar(200) NOT NULL,
  `Price` decimal(5,2) DEFAULT '0.00',
  `bestseller` int(1) NOT NULL DEFAULT '0'
)

这些是我的图书表中的记录

INSERT INTO `books` (`id`, `bookname`, `bookimage`, `author_id`, `publisher_id`, `published_date`, `genre`, `description`, `Price`, `bestseller`) VALUES
(1, 'The Pianist', 'book1.jpg', 4, 1, '04-07-2015', 'Fiction', 'The story of a Polish-Jewish pianist, when playing live on the radio in Warsaw when the station is bombed.', '200.00', 1),
(3, 'Beyond The Mask', 'book2.jpg', 2, 2, '04-17-2015', 'Art & Design', '‘I don’t see my work as extreme. I don’t really believe in traditional definitions of beauty,’ says photographer Roger Ballen.', '150.00', 1),
(17, 'hh', 'book1.jpg', 1, 0, '', 'hh', 'hh', '0.00', 0);

用户表记录

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `email` varchar(50) NOT NULL,
  `username` varchar(15) NOT NULL,
  `password` varchar(15) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `firstname`, `lastname`, `email`, `username`, `password`) VALUES
(1, 'admin', 'admin', 'admin@exm.com', 'admin', 'admin'),
(2, 'publisher', 'publisher', 'publisher@azteca.com', 'publisher', 'publisher'),
(3, 'member', 'member', 'member@exm.com', 'member', 'member'),
(4, 'author', 'author', 'author@exm.com', 'author', 'author');

现在,从管理界面我想获得此查询的所有记录

SELECT DISTINCT b.id as 'Book Id',b.bookname as 'Book Name',
                                            p.firstname as 'Publisher Fistname',p.lastname as 'Publisher Lastname',
                                            b.published_date as 'Published Date', b.genre as 'Genre',
                                            b.description as 'Description', b.price as 'Price', 
                                            b.bestseller as 'Best Seller'
                                            FROM books b, users p
                                            WHERE p.id = b.publisher_id
                                            ORDER BY b.publisher_id

但是对于这个查询,我没有看到id为17的书,请建议我如何获取所有记录,尽管publisher_id为0且published_date为空。

1 个答案:

答案 0 :(得分:0)

使用左连接而不是where条件(内连接)

尝试以下查询。

SELECT DISTINCT b.id AS 'Book Id',b.bookname AS 'Book Name',
    p.firstname AS 'Publisher Fistname',p.lastname AS 'Publisher Lastname',
    b.published_date AS 'Published Date', b.genre AS 'Genre',
    b.description AS 'Description', b.price AS 'Price', 
    b.bestseller AS 'Best Seller'
    FROM books b 
    LEFT JOIN users p ON p.id = b.publisher_id    
    ORDER BY b.publisher_id;

如果有帮助,那么markt就是答案..