1、将两条sql组合成一行显示可用union或者如下:
原表
SELECT COUNT(*) FROM tab_friend WHERE status=0 AND is_blacklist=1 AND my_id=7;
SELECT COUNT(*) FROM tab_friend WHERE status=0 AND is_blacklist=1 AND friend_id=7;
SELECT (SELECTCOUNT(*) FROM tab_friend WHERE status=0AND is_blacklist=1 AND my_id=7) AS att,
(SELECT COUNT(*) FROMtab_friend WHERE status=0 ANDis_blacklist=1 AND friend_id=7) AS fans from tab_friend LIMIT 0,1;
2、好友加本人union的用法
SELECT my_id FROMtab_friend tf WHERE friend_id=7 AND tf.status=0
UNION
select friend_id fromtab_friend WHERE friend_id=7
3、Brew数据后台四表联合查询
SELECTuserId,game.gameName,channel.channelName,goods.goodsName,buyRec.goodsNum,buyRec.goodsNum*goods.goodsPrice,buyRec.buyTime
FROMbuy_record buyRec
left join channelinfo channelonbuyRec.channelId=channel.channelId
left join goodsinfo goods ongoods.goodsId=buyRec.goodsId
left join gameinfo gameongame.gameId=buyRec.gameId;
概念:
inner join(等值连接) 只返回两个表中联结字段相等的行
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
4、没有购买过goodsId为2的用户id
SELECT userId FROM buy_record
WHEREuserId not in(SELECT userId FROM buy_record WHEREgoodsId=2)
1. LEFT JOIN:
(保证找出左联表中的所有行)
查出所有文章,并显示出他们的分类:
SELECT p.title,c.category_name FROM post p LEFT JOIN category c ON p.cid =c.cid
2. RIGHT JOIN:
(保证找出右联表中的所有行)
查询所有的分类,并显示出该分类所含有的文章数。
SELECT COUNT(p.id),c.category_name FROM post p RIGHTJOIN category c ON p.pid = c.cid
3. INNER JOIN
(找出两表中关联相等的行)
查询有所属分类的日志。(即那些没有所性分类的日志文章将不要我们的查询范围之内)。
SELECT p.title,c.category_name FROM post p INNER JOIN category c ON p.cid= c.cid.
这种情况和直接两表硬关联等价。
5、查找mysql 数据库 自动 添加 序号 字段列1,2,3,4..........
select (@rowNO := @rowNo+1) AS rowno,amount
from (SELECTamount FROM tab_goods where 1=1) a,(select @rowNO :=0) b ORDER BY amount DESC
6、节目名称有重复的现象,查询并删除之
/*查询节目名称有重复的*/
select * from tab_tv_detail group by program_namehaving count(program_name)>1
/*删除保存最大的值*/
delete from tab_tv_detail where detail_id not in(select * from (select min(detail_id) from tab_tv_detail group by program_code) astmp);