标签:
MYSQL作为最成功的开源关系型数据库之一,拥有大批的粉丝(本人也是),在这篇文章中,我们精心收集了10个最实用的mysql查询技巧,希望能够带给大家惊喜,如果大家也有非常不错的SQL,请留言与我们分享!
1. SELECT id,title,
2. (CASE date WHEN ‘0000-00-00‘ THEN ‘‘ ELSE date END) AS date
3. FROM your_table
4.
5. SELECT id,title,
6. (CASE status WHEN 0 THEN ‘open‘ WHEN 1 THEN ‘close‘ ELSE ‘standby‘ END) AS status
7. FROM your_table
1. SELECT email, COUNT(email) AS q
2. FROM emails_table GROUP BY email HAVING q > 1
3. ORDER BY q DESC
1. SELECT * FROM your_table ORDER BY RAND()
1. UPDATE your_table
2. SET name=REPLACE(name, ‘gbin1.com‘, ‘gbtags.com‘)
3. WHERE name LIKE ‘%John%‘;
1. ALTER TABLE your_table AUTO_INCREMENT = 2
下一次你插入数据,那么id自动增加为2
1. set @N = 0;
2. SELECT @N := @N +1 AS number, name, surname FROM gbtags_users;
1. SELECT CONCAT(name,‘ ‘,surname) AS complete_name FROM users
1. SELECT id,title, YEAR(date_field) FROM your_table
2. SELECT id,title,
3. CONCAT(MONTH(date_field),‘/‘,YEAR(date_field)) as new_date
4. FROM your_table
上面语句,第一个select取出了年份,第二取出了月份和年份组合
1. INSERT IGNORE INTO tags (tag) VALUES (‘good‘);
这个insert语句可以执行多次,不会报错,重复的插入会被忽略
1. select * from articles where MATCH(content_column) AGAINST (‘music‘)
你需要首先添加全文搜索索引到指定的列(content_column)。注意如果你表里已经有数据的话,不会创建索引,所以你需要使用一个空的表来执行这个语句
1. SELECT user, count(*) AS logins
2. FROM stat_log
3. WHERE action=‘LOGIN‘ AND dt_when >= DATE_ADD(CURDATE(), INTERVAL -1 MONTH)
4. GROUP BY user
这个语句能够让你查询字段dt_when一个月以前的所有记录
1. SET NAMES ‘utf8‘;
请在连接开始后执行这个语句
1. INSERT INTO yourtable (field1,field2,field3)
2. SELECT newfield1,newfield2,‘fixed value‘
3. FROM yourtable2
这个语句能够快速的帮助大家快速的添加一个表中的特定内容到另外一个表中。
标签:
原文地址:http://www.cnblogs.com/shengjie123/p/5692580.html