Php 获取预览数据的mysql查询
我有一个表,其中我有id和父id。在“管理”面板中,如果用户单击“预览”按钮,它将重定向到具有预览id的站点。当我们单击“预览”按钮时,数据将以“父id”键中的id保存在表中 我创建了查询,它工作正常,但它按顺序显示数据,但我想用预览行替换父行Php 获取预览数据的mysql查询,php,sql,wordpress,preview,Php,Sql,Wordpress,Preview,我有一个表,其中我有id和父id。在“管理”面板中,如果用户单击“预览”按钮,它将重定向到具有预览id的站点。当我们单击“预览”按钮时,数据将以“父id”键中的id保存在表中 我创建了查询,它工作正常,但它按顺序显示数据,但我想用预览行替换父行 select * from wp_content where page='sharepoint_features' and (status='1' or (status='preview' and parent_id='146')) and id<
select * from wp_content
where page='sharepoint_features'
and (status='1' or (status='preview' and parent_id='146'))
and id<>146
从wp\u内容中选择*
其中page='sharepoint\u features'
和(status='1'或(status='preview'和parent_id='146'))
和id146
这里是我们从表单预览id获得的父id
通过这个查询,我们得到了以下数据
在上表中,ID1303应位于145之后,您必须添加一个
ORDER BY
子句
试试这个:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by coalesce(CAST(parent_id as INT), id)
更新2
尝试此查询,并告诉它是否正常:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by
case
when parent_id is null then id
else CONVERT(cast parent_id as INT)
end
从wp\u内容中选择*
其中page='sharepoint\u features'
和(状态='1'
或者(status='preview'和parent_id='146'))
和id146
订购人
案例
当parent_id为null时,则为id
else转换(将父项\u id转换为INT)
结束
您必须添加一个ORDER BY
子句
试试这个:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by coalesce(CAST(parent_id as INT), id)
更新2
尝试此查询,并告诉它是否正常:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by
case
when parent_id is null then id
else CONVERT(cast parent_id as INT)
end
从wp\u内容中选择*
其中page='sharepoint\u features'
和(状态='1'
或者(status='preview'和parent_id='146'))
和id146
订购人
案例
当parent_id为null时,则为id
else转换(将父项\u id转换为INT)
结束
以下是一个基于@JoeTaras解决方案的可能解决方案:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by coalesce(NULLIF(parent_id, ''), id)
从wp\u内容中选择*
其中page='sharepoint\u features'
和(状态='1'
或者(status='preview'和parent_id='146'))
和id146
按合并排序(NULLIF(父项id“”),id)
我假设的是您的数据结构。这里有一个基于@JoeTaras解决方案的可能解决方案:
select * from wp_content
where page='sharepoint_features'
and (status='1'
or (status='preview' and parent_id='146'))
and id<>146
order by coalesce(NULLIF(parent_id, ''), id)
从wp\u内容中选择*
其中page='sharepoint\u features'
和(状态='1'
或者(status='preview'和parent_id='146'))
和id146
按合并排序(NULLIF(父项id“”),id)
我假设的是您的数据结构。得到相同结果的joeparent\u id列是varchar类型,所以我使用了order by coalesce(cast(
parent\u id
as INT),id),但仍然得到same@vishal当前位置亲爱的,你看到我的sql小提琴了吗?还好吗?是的,工作正常,mysql版本有问题。我有mysql 5.0.12。亲爱的,Mysql 5.0.12是一个旧版本(2005年),可能由于得到相同的结果而导致了顺序问题,joeparent_id列是varchar类型,所以我使用了ORDER by coalesce(cast(parent_id
as INT),id),但仍然得到same@vishal当前位置亲爱的,你看到我的sql小提琴了吗?还好吗?是的,工作正常,mysql版本有问题。我有mysql 5.0.12。亲爱的,Mysql 5.0.12是一个旧版本(2005年)。也许ORDER ByThank@CUGreen有问题,但它不起作用。parent_id为varchar,因此字段将为null或空。@vishal,再次说明,您是否检查了指向我更新的sql FIDLE的链接?它的父id为varchar。我们可以假设是您的mysql版本导致了这里的问题吗?是的,我有mysql版本5.0.12。版本问题。但我想在我的versionTanks@CUGreen上运行该查询,但它不起作用。parent_id为varchar,因此字段将为null或空。@vishal,再次说明,您是否检查了指向我更新的sql FIDLE的链接?它的父id为varchar。我们可以假设是您的mysql版本导致了这里的问题吗?是的,我有mysql版本5.0.12。版本问题。但我想在我的版本上运行该查询检查新试用检查新试用