我正在使用目录网站,用户可以在其中浏览类别。类别可以包含其他类别和产品,产品可以属于多个类别。相关的数据库模式如下所示:
CREATE TABLE products (
product_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
product_title VARCHAR(100) NOT NULL,
product_status TINYINT UNSIGNED NOT NULL
);
CREATE TABLE product_categories (
category_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
parent_category_id INT UNSIGNED NOT NULL,
category_title VARCHAR(100) NOT NULL,
category_status TINYINT UNSIGNED NOT NULL,
category_order INT UNSIGNED NOT NULL
);
CREATE TABLE products_categories (
product_id INT UNSIGNED NOT NULL,
category_id INT UNSIGNED NOT NULL,
product_order INT UNSIGNED NOT NULL,
PRIMARY KEY(product_id, category_id)
);
我遇到的问题是我需要使用LIMIT n, n
$perpage = 20;
$start = (isset($_GET['page'])) ? (int)$_GET['page'] * $perpage : 1;
$limitsql = "LIMIT $start, $perpage";
但我无法弄清楚如何在不加入和合并结果的情况下选择不同的类别和产品。理想情况下,我希望得到这样的结果:
product_id | product_title | category_id | category_title
NULL | NULL | 32 | category foo
NULL | NULL | 239 | category bar
9391 | product foo | NULL | NULL
325 | product bar | NULL | NULL
我能做的最好的事情就是得到这样的东西,但这并没有多大帮助:
product_id | product_title | category_id | category_title
9391 | product foo | 32 | category foo
325 | product bar | 239 | category bar
239 | product foo2 | 32 | category foo
115 | product bar2 | 239 | category bar
我能想到的唯一其他解决方案是查询类别中的所有子类别和产品,将它们粘贴到php数组中并使用array_slice
提取当前页面。考虑到产品的数量(数千),这不是一个非常吸引人的选择。
否则我可以查询类别数量,并将$start
子句中的LIMIT
偏移类别数。如果有超过一整页的类别,这会变得混乱。
这是我当前的工作查询,它给出了上面的结果:
SELECT
p.product_id, p.product_title,
c.category_id, c.category_title
FROM products AS p
JOIN product_categories AS c
ON c.parent_category_id='20'
INNER JOIN products_categories AS pc
ON p.product_id=pc.product_id
WHERE p.product_status='1' AND pc.category_id='20'
ORDER BY pc.product_order ASC
我想我已经使用了UNION
,我完全忘记了
SELECT
c.category_id AS row_id, c.category_title AS row_title, 1 AS is_category
FROM product_categories AS c
WHERE c.parent_category_id='20'
UNION
SELECT
p.product_id AS row_id, p.product_title AS row_title, 0 AS is_category
FROM products AS p
INNER JOIN products_categories AS pc
ON p.product_id=pc.product_id
我猜联盟不会按照我的想法行事。由于两者都被视为单独的查询,因此我无法将LIMIT
应用于整个结果,而只应用于每个SELECT
。此外,从每个语句中选择的列似乎必须与另一个语句中相应类型的类型相同。
答案 0 :(得分:6)
使用:
SELECT *
FROM (SELECT c.category_id AS row_id, c.category_title AS row_title, 1 AS is_category
FROM product_categories AS c
WHERE c.parent_category_id='20'
UNION
SELECT p.product_id AS row_id, p.product_title AS row_title, 0 AS is_category
FROM products AS p
JOIN products_categories AS pc ON p.product_id=pc.product_id) x
LIMIT x, y
答案 1 :(得分:0)
您可以采用的另一种方法是更改您的架构,使类别和产品本质上相同。
CREATE TABLE items (
item_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
item_title VARCHAR(100) NOT NULL,
item_status TINYINT UNSIGNED NOT NULL,
category_or_item TINYINT UNSIGNED NOT NULL,
);
CREATE TABLE items_parents (
item_id INT UNSIGNED NOT NULL,
parent_id INT UNSIGNED NOT NULL, #points to itemid
item_order INT UNSIGNED NOT NULL,
PRIMARY KEY(item_id, parent_id)
);
您的查询是平的,您可以按category_or_item对其进行排序,以便首先显示类别。