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

时间:2014-11-13 17:05:40

标签: php mysql sql

我有一张推荐书表,名为'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个推荐。 我想你会明白我在说什么?

4 个答案:

答案 0 :(得分:0)

我会这样做:

SELECT  * FROM testimonials_manager ORDER BY testimonials_image

答案 1 :(得分:0)

SELECT *
FROM testimonials_manager
ORDER BY testimonials_image

为了让NULL testimonials_image值转到底部,但其余的结果以升序显示,您可以只在order by子句中替换NULL。

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

答案 2 :(得分:0)

在修复你的小提琴后,我运行了这个查询 -

SELECT *
FROM testimonials_manager
ORDER BY testimonials_image DESC

EXAMPLE

您也可以这样做 -

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

答案 3 :(得分:0)

是的,经过多次尝试后我得到了答案......符合我要求的查询如下:

SELECT * FROM `testimonials_manager` order by `testimonials_image` IS NULL DESC, `testimonials_image` DESC
相关问题