版本环境:MySql5.5以上
同样的SQL语句,用来创建视图排序就变乱了,而如果直接执行语句就非常正常,请高手指教,E-mail:[[email protected]][/email]结果如下图:
测试数据建立代码如下:
-- --------------------------------------------------------
-- Host:                         127.0.0.1
-- Server version:               5.5.21-log - MySQL Community Server (GPL)
-- Server OS:                    Win64
-- HeidiSQL version:             7.0.0.4140
-- Date/time:                    2012-05-27 18:17:25
-- --------------------------------------------------------/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;
-- Dumping structure for table test.expire_record
DROP TABLE IF EXISTS `expire_record`;
CREATE TABLE IF NOT EXISTS `expire_record` (
  `ExpireRecord_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `GasDetector_ID` int(10) unsigned NOT NULL,
  `RecordStatueType_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`ExpireRecord_ID`),
  KEY `FK_GasDetector_ID_5` (`GasDetector_ID`),
  KEY `FK_RecordStatue_Type_ID_4` (`RecordStatueType_ID`),
  CONSTRAINT `FK_GasDetector_ID_5` FOREIGN KEY (`GasDetector_ID`) REFERENCES `gasdetector_info` (`GasDetector_ID`),
  CONSTRAINT `FK_RecordStatueType_ID_4` FOREIGN KEY (`RecordStatueType_ID`) REFERENCES `recordstatue_type` (`RecordStatueType_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1;-- Dumping data for table test.expire_record: ~12 rows (approximately)
DELETE FROM `expire_record`;
/*!40000 ALTER TABLE `expire_record` DISABLE KEYS */;
INSERT INTO `expire_record` (`ExpireRecord_ID`, `GasDetector_ID`, `RecordStatueType_ID`) VALUES
(1, 1, 2),
(2, 1, 3),
(3, 2, 2),
(4, 3, 1),
(5, 4, 1),
(6, 5, 1),
(7, 7, 1),
(8, 8, 1),
(9, 13, 1),
(10, 15, 1),
(11, 16, 1),
(12, 17, 1);
/*!40000 ALTER TABLE `expire_record` ENABLE KEYS */;
-- Dumping structure for table test.gasdetector_info
DROP TABLE IF EXISTS `gasdetector_info`;
CREATE TABLE IF NOT EXISTS `gasdetector_info` (
  `GasDetector_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`GasDetector_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;-- Dumping data for table test.gasdetector_info: ~20 rows (approximately)
DELETE FROM `gasdetector_info`;
/*!40000 ALTER TABLE `gasdetector_info` DISABLE KEYS */;
INSERT INTO `gasdetector_info` (`GasDetector_ID`) VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12),
(13),
(14),
(15),
(16),
(17),
(18),
(19),
(20);
/*!40000 ALTER TABLE `gasdetector_info` ENABLE KEYS */;
-- Dumping structure for table test.recordstatue_type
DROP TABLE IF EXISTS `recordstatue_type`;
CREATE TABLE IF NOT EXISTS `recordstatue_type` (
  `RecordStatueType_ID` tinyint(3) unsigned NOT NULL,
  `Statue` char(4) NOT NULL,
  PRIMARY KEY (`RecordStatueType_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- Dumping data for table test.recordstatue_type: ~4 rows (approximately)
DELETE FROM `recordstatue_type`;
/*!40000 ALTER TABLE `recordstatue_type` DISABLE KEYS */;
INSERT INTO `recordstatue_type` (`RecordStatueType_ID`, `Statue`) VALUES
(1, '未处理'),
(2, '正在处理'),
(3, '处理完成'),
(4, '自动恢复');
/*!40000 ALTER TABLE `recordstatue_type` ENABLE KEYS */;
-- Dumping structure for view test.view_expire
DROP VIEW IF EXISTS `view_expire`;
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `view_expire` (
`GasDetector_ID` INT(10) UNSIGNED NOT NULL,
`s1` VARCHAR(4) NOT NULL COLLATE 'utf8_general_ci'
) ENGINE=MyISAM;
-- Dumping structure for view test.view_expire
DROP VIEW IF EXISTS `view_expire`;
-- Removing temporary table and create final VIEW structure
DROP TABLE IF EXISTS `view_expire`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` VIEW `view_expire` AS SELECT gi.GasDetector_ID, 
IFNULL(rt.statue,'暂不处理') AS s1
FROM gasdetector_info AS gi
LEFT JOIN expire_record AS er using (GasDetector_ID)
LEFT JOIN RecordStatue_Type AS rt USING (RecordStatueType_ID)
ORDER BY field (s1,'未处理','正在处理','暂不处理','处理完成')
,gi.GasDetector_ID ;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

解决方案 »

  1.   

    你把ORDER BY field (s1,'未处理','正在处理','暂不处理','处理完成')
    ,gi.GasDetector_ID ;改成ORDER BY gi.GasDetector_ID ;应该可以看到视图的结果已经是乱码了  难道field函数放在视图里面有bug?
      

  2.   

    乱码是你字符集的问题吧,我们的要求是按状态来排序,未处理的最紧急,排在最前面,然后是正在处理,然后是暂不处理,最后是处理完成。数据库的字符集要用UTF8
      

  3.   

    难道你那边在视图里面去掉field  显示正常?