Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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
Mysql 即使某些引用为空,也要对数据进行排序_Mysql_Sql_Database_Null_Sql Order By - Fatal编程技术网

Mysql 即使某些引用为空,也要对数据进行排序

Mysql 即使某些引用为空,也要对数据进行排序,mysql,sql,database,null,sql-order-by,Mysql,Sql,Database,Null,Sql Order By,我想在名字后面排序结果。因此,需要多个表。现在的问题是,即使列中有null,也要对名称进行排序。下面是一个示例数据库,它应该代表问题: 我的桌子 CREATE TABLE IF NOT EXISTS `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `desc` varchar(255) NOT NULL, `price` decimal(10,0) NOT NULL, `manufacturer_id` int(11) DEF

我想在名字后面排序结果。因此,需要多个表。现在的问题是,即使列中有
null
,也要对名称进行排序。下面是一个示例数据库,它应该代表问题:

我的桌子

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `desc` varchar(255) NOT NULL,
  `price` decimal(10,0) NOT NULL,
  `manufacturer_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

CREATE TABLE IF NOT EXISTS `manufacturer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `desc` varchar(255) NOT NULL,
  `website` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
我的数据:

INSERT INTO `products` (`id`, `desc`, `price`, `manufacturer_id`) VALUES
(1, 'book', 12, 1),
(2, 'cup', 4, 2),
(3, 'Arbitrary product', 100, NULL);

INSERT INTO `manufacturer` (`id`, `title`, `desc`, `website`) VALUES
(1, 'Publisher', 'Lorem ipsum', 'www.stackoverflow.com'),
(2, 'Cup producer', 'Lorem ipsum', 'www.cup.com');
如果我做一个
SELECT*FROM products
,我会得到三个结果。如果我想订购,我有一个查询

SELECT p.desc, p.price, m.title
FROM products p, manufacturer m
WHERE p.manufacturer_id = m.id
ORDER BY m.title
由于
products
中的
null
值,这只给出了两个结果。即使表中有
null
,也可以在制造商名称后对表
产品进行排序吗?

使用

试试这个:

SELECT p.desc, p.price, m.title
FROM products p LEFT OUTER JOIN manufacturer m
ON p.manufacturer_id = m.id
ORDER BY m.title

你说p.manufacturer\u id=m.id做了一个连接 但是您没有指定它,所以默认情况下它是一个内部连接, 您希望在“左”表是products表的地方有一个左联接

SELECT p.desc, p.price, m.title
FROM products AS p
LEFT JOIN manufacturer AS m ON m.id = p.manufacturer_id
ORDER BY m.title
为了更好地理解,请查看此图像