1

这是我正在处理的查询:

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 列是冲突点,其余的模式都很好。

据我所知c.ordering,应该首先订购为什么第 1 列和第 2 列是混乱的?

**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)

**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)


这是我的问题的简化版本,谢谢大家的时间。请运行以下 sql 代码片段

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);  

请运行下面的查询来模拟我的问题

  SELECT catid  
  FROM mc23r_k2_items as i  
  LEFT JOIN mc23r_k2_categories AS c  
  ON c.id = i.catid  
  WHERE  c.id IN (1,2,3,4)  
  ORDER BY c.ordering, i.ordering  
  LIMIT 0, 100  

结果集不是由 catid 排序的

4

1 回答 1

0

查询实际上是按照它的意图进行的。问题在于 c.ordering 值,在我的项目中,父类别具有相同的排序值 (1),其第一个子类别具有相同的排序值 (1)
感谢大家的宝贵时间@nawfal。

于 2012-11-02T15:48:45.490 回答