>SELECT id FROM posts AS post_id;SET categories.id AS category_id;
>SELECT title, contents, date_posted, categories, name FROM posts INNER JOIN
>categories ON categories, id = posts, cat_id ORDER BY id DESC
并收到错误消息 $#1193 - 未知的系统变量' id'
我也尝试这样的事情:
>SELECT id FROM posts AS post_id;
>SELECT id FROM categories AS category_id;
>SELECT COALESCE (title, contents, date_posted, categories, name)
>FROM posts INNER JOIN categories ON categories, id = posts, cat_id
>ORDER BY id DESC
许多其他工作人员都没有正常工作。
工作查询:
>SELECT posts . p_id AS post_id, categories . c_id AS category_id,
>title, contents, date_posted, categories . name
>FROM posts
>INNER JOIN categories ON categories . c_id = posts . cat_id
>ORDER BY posts.p_id DESC;
答案 0 :(得分:0)
如果我理解您的查询,请按以下方式使用 -
create table #ordered_products
(id int identity(1,1),
grouping int default 0,
Description varchar(128),
level int,
is_active bit);
insert into #ordered_products (Description, level, is_active)
select description, level, is_active from products;
-- All rows now have an order
declare @grouping int = 0;
declare @currentid int;
declare @found bit = 1;
declare @level int;
declare @base int;
set @currentid = (select min(id) from #ordered_products);
-- Go through all the rows setting a new group each time we hit level 1
while (@found = 1)
begin
set @level = (select level from #ordered_products where id = @currentid);
if (@level = 1)
begin
set @grouping = @grouping + 1
end
update #ordered_products set grouping = @grouping where id = @currentid
set @currentid = @currentid + 1;
set @found = (select 1 from #ordered_products where id = @currentid);
end
-- For each group, set the children to be inactive if the parent is
declare @maxgroup int = (select MAX(GROUPING) from #ordered_products);
declare @currentgroup int = 1;
while (@currentgroup <= @maxgroup)
begin
begin
set @base = (select MIN(id) from #ordered_products where is_active = 0 and grouping = @currentgroup);
if (@base > 0)
begin
update #ordered_products set is_active = 0 where grouping = @currentgroup and id > @base;
end
end
set @currentgroup = @currentgroup + 1;
end
-- Output
select * from #ordered_products where is_active = 1;
drop table #ordered_products;
注意:您可以根据cat.id进行订购,然后相应地进行更改。
答案 1 :(得分:0)
尝试将数据库中的变量名称ID更改为其他名称。 id是保留名称。