以下是我正在处理的查询:
SELECT i.*,
CASE WHEN i.modified = 0 THEN i.created ELSE i.modified END as lastChanged, c.name as categoryname,c.id as categoryid, c.alias as categoryalias, c.params as categoryparams
FROM mc23r_k2_items as i
LEFT JOIN mc23r_k2_categories AS c
ON c.id = i.catid
WHERE i.published = 1
AND c.id IN (1,2,3,4)
ORDER BY c.ordering, i.ordering
LIMIT 0, 100
我是加入和高级东西的新手。 上述查询的结果是返回catid,如下所示:
c.ordering: 2 1 2 1 2 1 2 2 2 3 3 3 4 4 4 4 4
i.ordering: 1 1 2 2 3 3 4 5 6 1 2 3 1 2 3 4 5
我正在寻找订单
c.ordering: 1 1 1 2 2 2 2 2 2 3 3 3 4 4 4 4 4
i.ordering: 1 2 3 1 2 3 4 5 6 1 2 3 1 2 3 4 5
我应该对查询做出哪些更改。只有c.ordering
列1和2是冲突点,其余模式是好的。
据我所知{1}}应首先订购为什么第1列和第2列混乱?
c.ordering
**mc23r_k2_items**
id INT(11)
title VARCHAR(255)
alias VARCHAR(255)
catid INT(11)
published SMALLINT(6)
introtext MEDIUMTEXT
fulltext MEDIUMTEXT
video TEXT
gallery VARCHAR(255)
extra_fields TEXT
extra_fields_search TEXT
created DATETIME
created_by INT(11)
created_by_alias VARCHAR(255)
checked_out INT(10)
checked_out_time DATETIME
modified DATETIME
modified_by INT(11)
publish_up DATETIME
publish_down DATETIME
trash SMALLINT(6)
access INT(11)
ordering INT(11)
featured SMALLINT(6)
featured_ordering INT(11)
image_caption TEXT
image_credits VARCHAR(255)
video_caption TEXT
video_credits VARCHAR(255)
hits INT(10)
以下是我的问题的简化版,感谢您的所有时间。请运行以下sql代码段
**mc23r_k2_categories**
id INT(11)
name VARCHAR(255)
alias VARCHAR(255)
description TEXT
parent INT(11)
extraFieldsGroup INT(11)
published SMALLINT(6)
access INT(11)
ordering INT(11)
image VARCHAR(255)
params TEXT
trash SMALLINT(6)
plugins TEXT
language CHAR(7)
请运行以下查询以模拟我的问题
CREATE DATABASE `test1` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `test1`;
CREATE TABLE IF NOT EXISTS `mc23r_k2_categories` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ordering` (`ordering`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
INSERT INTO `mc23r_k2_categories` (`id`, `ordering`) VALUES
(1, 1),
(2, 1),
(3, 2),
(4, 3),
(5, 2),
(6, 1),
(7, 2),
(8, 3),
(9, 4),
(10, 1),
(11, 2),
(12, 3),
(13, 4),
(14, 5),
(15, 6);
CREATE TABLE IF NOT EXISTS `mc23r_k2_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) NOT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `catid` (`catid`),
KEY `ordering` (`ordering`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
INSERT INTO `mc23r_k2_items` (`id`, `catid`, `ordering`) VALUES
(1, 2, 1),
(2, 2, 2),
(3, 2, 3),
(4, 2, 4),
(5, 2, 5),
(6, 2, 6),
(7, 3, 1),
(8, 3, 2),
(9, 3, 3),
(10, 3, 4),
(11, 4, 1),
(12, 4, 2),
(13, 4, 4),
(14, 4, 3),
(15, 4, 5),
(16, 1, 2),
(17, 1, 3),
(18, 1, 1);
结果集未按catid排序
答案 0 :(得分:0)
查询实际上是在做它想要的。问题在于c.ordering值在我的项目父类别中具有相同的排序值(1)并且其第一个子类别具有相同的排序值(1)
感谢大家为你宝贵的时间@nawfal。