SQL statement used in sqls. SQL statement

Source: Internet
Author: User

SQL statement used in sqls. SQL statement

Sqlite has been used for many queries recently, including table connection queries. Record it to avoid forgetting it!


1. First sort by time and then select the first 10:

Select * from MyBill order by createTime desc limit 10;

2. Sum of price:

Select sum (price) as sum from MyBill;

3. There is a category table and a bill table, which is sorted by the primary category statistics in the category:

Select sum (mysum), tb. pid from"
+ "(Select sum (price) as mysum, consumetype. title as tt, consumetype. parentid as pid from mybill"
+ "Left join consumetype on mybill. typeid = consumetype. id"
+ "Group by mybill. typeid) as tb group by tb. pid order by sum (mysum) desc ";

4. There is a category table and a bill table, which is sorted by subcategory statistics in the category:

Select sum (price), consumetype. title from mybill"
+ "Left join consumetype on mybill. typeid = consumetype. id"
+ "Group by mybill. typeid order by sum (price) desc ";

5. Compare and select Based on Time:

Select * from MyBill where createTime <datetime ('2017-05-19 ')

The corresponding time functions are as follows:

* Select datetime ('now ');
* Select datetime ('2017-10-17 00:20:00 ',' + 1 hour ','-12 minute ');
* Select date ('2014-10-17 ',' + 1 Day', '+ 1 year ');
* Select datetime ('now ', 'start of year ');
* Select datetime ('now ', 'start of month ');
* Select datetime ('now ', 'start of Day ');
* Select datetime ('now ',' + 10 hour ', 'start of Day',' + 10 hour ');
* Select datetime ('now ', 'localtime ');
* Select datetime ('now ',' + 8 hour ');



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.