1. 建表SQL ``sql CREATE TABLE IF NOT EXISTSarticle(idINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,author_idINT(10)UNSIGNED NOT NULL,category_idINT(10) UNSIGNED NOT NULL,viewsINT(10) UNSIGNED NOT NULL,commentsINT(10) UNSIGNED NOT NULL,titleVARBINARY(255) NOT NULL,content` TEXT NOT NULL );

    INSERT studey_mysql.article (author_id, category_id, views, comments , title , content ) VALUES (1,1,1,1,’1’,’1’), (2,2,2,2,’2’,’2’), (1,1,3,3,’3’,’3’);

    1. 2. 检索 , 创建索引
    2. ```sql
    3. # 查询 category_id 为1 且 comments 大于1 的情况下 , views最多的 article_id
    4. explain
    5. select * from article
    6. where category_id=1 and comments >1
    7. order by views desc
    8. limit 0,1
    9. #创建索引( 测试之后 不合适 )
    10. create index idx_article_ccv on article(category_id,comments,views);
    11. #查看索引
    12. show index from article
    13. #删除不合适的索引
    14. drop index idx_article_ccv on article;
    15. #重新创建
    16. create index idx_article_cv on article(category_id,views);
    1. 知识点 ```
    2. where后面的就需要添加索引

    3. 如果字段是范围查询就不用加索引 ; 例如 < , > ```