SELECT COUNT(*) FROM dbtest.`orders` a WHERE a.order_name='admin'
可以返回符合条件记录的总数
例:
SELECT * FROM dbtest.`orders`;
SELECT COUNT(*) FROM dbtest.`orders` a WHERE a.order_name='admin'
SELECT COUNT(*) FROM dbtest.`orders` a WHERE a.order_name='admin1'
可以返回符合条件记录的总数
SELECT COUNT(1) FROM dbtest.`orders` a WHERE a.order_name='admin'
SELECT COUNT(1) FROM dbtest.`orders` a WHERE a.order_name='admin1'
select 1 from tablename where col = col limit 1;
然后读取语句执行所影响的行数。
当然这里limit 1很重要。这要mysql找到一条记录后就不会在往下找了。这里执行所影响的行数不是0就是1,性能提高了不少。
例:
SELECT 1 FROM dbtest.`orders` a WHERE a.order_name='admin';
SELECT 1 FROM dbtest.`orders` a WHERE a.order_name='admin1';
SELECT 1 FROM dbtest.`orders` a WHERE a.order_name='admin' LIMIT 1;
SELECT 1 FROM dbtest.`orders` a WHERE a.order_name='admin1' LIMIT 1;
SELECT * FROM `feed` WHERE `uid` =1121293 limit 1
可以返回一条记录的字段内容
SELECT EXISTS(SELECT * FROM dbtest.`orders` a WHERE a.order_name='admin')
只能判断是否存在记录,子查询中的SELECT *在执行时会被忽略掉。
SELECT EXISTS(SELECT * FROM dbtest.`orders` a WHERE a.order_name='admin1')
INSERT INTO test(id, name) SELECT id, name FROM tset1 WHERE not exists (select * from test where tset.id = tset1.id);