Mysql 这样可以一句sql出来吗
-- phpMyAdmin SQL Dump
-- version 2.10.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2013 年 01 月 25 日 06:32
-- 服务器版本: 5.0.45
-- PHP 版本: 5.2.3
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- 数据库: `test`
--
-- --------------------
--
-- 表的结构 `location`
--
CREATE TABLE `location` (
`location_id` int(10) unsigned NOT NULL,
`score` int(10) unsigned NOT NULL,
`welcome` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`location_id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
--
-- 导出表中的数据 `location`
--
INSERT INTO `location` VALUES (1000, 50, 1);
INSERT INTO `location` VALUES (1001, 60, 0);
INSERT INTO `location` VALUES (1002, 30, 1);
INSERT INTO `location` VALUES (1003, 20, 0);
排序要求:
如果 welcome 为1 则按score + 50 来排
例查询两条数据 按 score DESC
结果为
location_id为1000和1002的两条数据
求助 感谢 !
mysql
------解决方案--------------------
这样写
select location_id, if(welcome=1,score+50,score) as score, welcome from location order by score DESC
Array
(
[0] => Array
(
[location_id] => 1000
[score] => 100
[welcome] => 1
)
[1] => Array
(
[location_id] => 1002
[score] => 80
[welcome] => 1
)
[2] => Array
(
[location_id] => 1001
[score] => 60
[welcome] => 0
)
[3] => Array
(
[location_id] => 1003