ここに私が取り組んでいるクエリがあります:
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
クエリにどのような変更を加える必要がありますか。列 1 と 2のみc.ordering
が競合のポイントです。残りのパターンは良好です。
私の知る限り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 順ではありません