mysql中union和union all的区别和注意点
昨天有个功能要实现排行效果,而且是随机按照某些字段的总值,但是第一个又要不一样,因为第一个人给了钱,所以要排第一。
打个比方吧,表(userinfo)中有这几个字段:username,isvalid,givedmoney,sumip,dayip,monthip,visitcount,regdate
现在要实现显示排行前10位用户,
出现在排行第一位的要是givedmoney为1的用户(表示给了钱), 而且isvalid为1(表示通过认证)
其它的9个用户呢,要在字段(sumip,dayip,monthip,visitcount)中随机选择一个作为排序的依据
这样的sql语句改怎么写呢?
下面是我的答案:
<?php
$arr_orderby=array("sumip","dayip","monthip","visitcount");
$orderby_filed=$arr_orderby(array_rand($arr_orderby) ); //得到随机字段
$sql="(select username,".$orderby_filed." from userinfo where isvalid=1 and givedmoney=1 limit 1) union (select username,".$orderby_filed." from userinfo where isvalid=1 and givedmoney!=1 limit 9) order by rand(),".$orderby_filed." desc";
?>
这里需要注意的几点是,
1:union前后的select字段一定要一样,而且顺序要一样,如果你不是select * 的话
2:第二个select中,试试limit 9和limit 10有什么不一样的效果
union和union all的区别就是后面能踢出重复的结果,如果确定表中没有重复的字段,建议使用union all
一 UNION语法
代码 复制代码
SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
UNION用于把来自许多
相关文档:
Link from : http://www.5ilinux.com/blog/archives/000091.html
1.首先我们建立相应的用户和用户组
groupadd -g 5500 ftpgroup
adduser -u 5500 -s /bin/false -d /bin/null -c "proftpd user" -g ftpgroup ftpuser
2.操作数据库
mysql mysql -uroot -ppassword
create database ftpdb
grant select, upd ......
SOURCE: CLICK HERE
本文讲述SQL Server、Oracle、MySQL查出值为NULL的替换。
在SQL Server Oracle MySQL当数据库中查出某值为NULL怎么办?
1、MSSQL: ISNULL()
语法
ISNULL ( check_expression , replacement_value )
参数
check_expression
将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。
re ......
写入时,先做encode:
public static String encode(String src) {
String result = null;
try {
result = new String(src.getBytes("gbk"), "ISO-8859-1");
} catch (UnsupportedEncodingException uee) {
System.err.println(uee);
}
return result;
}
读出时,再做decode:
public static String deco ......
Some problems encounted this afternoon, when trying to setup/test mysql capabilities on hypnos and virgil.
1.ERROR 2003 (HY000): Can't connect to MySQL server on 'server-name' (111)
access locally (i.e. from localhost) is fine, when trying remotely got this error. when tried with mysql++ library, ......