繁体   English   中英

根据特定列中存在的值对表进行排序

[英]Sort a table based on a value present in a specific column

我有一个名为“ testimonials_manager”的推荐表,并且存在一个名为“ testimonials_image”的列,用于存储推荐图像的路径和名称。

我想显示我的推荐列表和列表排序基于一个图像值,这意味着最先显示包含图像的推荐列表,最后显示不包含图像的列表。

是否可以根据特定列中存在的值对表进行排序? 请帮我。

我的表结构是:

CREATE TABLE IF NOT EXISTS `testimonials_manager` (
  `testimonials_id` int(11) NOT NULL auto_increment,
  `language_id` int(11) NOT NULL default '0',
  `testimonials_title` varchar(64) collate utf8_unicode_ci NOT NULL default '',
  `testimonials_url` varchar(255) collate utf8_unicode_ci default NULL,
  `testimonials_name` text collate utf8_unicode_ci NOT NULL,
  `testimonials_image` varchar(254) collate utf8_unicode_ci NOT NULL default '',
  `testimonials_html_text` text collate utf8_unicode_ci,
  `testimonials_mail` text collate utf8_unicode_ci NOT NULL,
  `testimonials_company` varchar(255) collate utf8_unicode_ci default NULL,
  `testimonials_city` varchar(255) collate utf8_unicode_ci default NULL,
  `testimonials_country` varchar(255) collate utf8_unicode_ci default NULL,
  `testimonials_show_email` char(1) collate utf8_unicode_ci default '0',
  `sidebox` int(1) NOT NULL default '0',
  `status` int(1) NOT NULL default '0',
  `date_added` datetime NOT NULL default '0000-00-00 00:00:00',
  `testimonial_add` date NOT NULL,
  `last_update` datetime default NULL,
  `testimonial_products` varchar(300) collate utf8_unicode_ci NOT NULL default '0',
  `testimonial_categories` varchar(300) collate utf8_unicode_ci NOT NULL default '0',
  PRIMARY KEY  (`testimonials_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

假设我已经在表中插入了50个推荐词,其中30个推荐词包含图像,其余20个推荐词没有图像。 我想首先在我的推荐列表中显示30个包含图像的推荐,然后显示那20个没有图像的推荐。 我想你会明白我在说什么吗?

我会那样做:

SELECT  * FROM testimonials_manager ORDER BY testimonials_image
SELECT *
FROM testimonials_manager
ORDER BY testimonials_image

为了使NULL testimonials_image值排在最底端,但其余结果按升序显示,您可以仅按by子句的顺序替换NULL。

SELECT *
FROM testimonials_manager
ORDER BY IFNULL(testimonials_image, 'zzz')

解决您的小提琴后,我运行了此查询-

SELECT *
FROM testimonials_manager
ORDER BY testimonials_image DESC

您也可以这样做-

SELECT *
FROM testimonials_manager
ORDER BY ISNULL(testimonials_image), testimonials_image desc

是的,尝试了很多之后我得到了答案...满足我要求的查询如下:

SELECT * FROM `testimonials_manager` order by `testimonials_image` IS NULL DESC, `testimonials_image` DESC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM