After successful execution, you can insert three records in the datasheet $users. Sometimes we need to insert a record and get our own ID, and we can use the Insertgetid method:
If we want to delete the entire datasheet data, omit the where condition, and if you want to empty the datasheet and set the self ID to 0, you can use the Truncate method:
Db::table (' users ')->truncate ();
Basic Query
4.1 Get all table records
Use the Get method to obtain all the records for a table:
If the database contains more than one piece of data, a one-time fetch can greatly affect performance, and correspondingly, we can call the chunk method block to get the data:
Here we specify to take two records at a time. The comment section means that we can set the query exit condition, and when that condition is reached, the query exits and no longer executes.
4.4 Getting a Single-column value
The query result obtained by the above method is an object instance/object instance array, sometimes we just want to get a single column of values, traversing the array to get the value of the specified column too troublesome, you can use the lists method to get a column value array:
A connection query is a query that associates two or more tables together to get data that matches the rows of one table with the rows of another. Common connection queries include inner joins (equivalent connections), left (outside) connections, right (outside) connections, and cross joins (full connections).
1.1 Internal connections
The inner join is used to get the intersection of two table result sets, and we can use the Query Builder join method for the INNER join query:
The left-connected result set specifies all rows of the left table, and if a row in the left table does not have a matching row in the right table, all picklist columns in the right table in the associated result set row are null (NULL). We use the Query Builder's Leftjoin method for the left-join query:
Many times, the query conditions of the connection query are often more complex, not a simple Join/leftjoin method can be done, then how do we add more complex query conditions. Use JoinClause instead of condition parameters:
A union query is used to combine the result sets of two or more queries into a single result set that contains all the rows of all queries in a federated query. The result set column name of the union is the same as the column name of the result set of the first SELECT statement in the Union operator, and the result set column names of the other SELECT statement are ignored, and the number of other query fields must be the same as the first. In the Laravel Query Builder we use the Union method for federated queries:
You can add a custom query condition by using the Where method on the query builder, which requires passing in three parameters: the first column name, the second is the operator, and the third is the comparison value:
Note that the Query Builder supports the method chain, which means that if there are multiple query conditions and the multiple conditions are connected, you can use more than one where method before get. If multiple conditions are connected using or, you can use the Orwhere method:
$user = db::table (' users ')->where (' name ', ' Laravel ')->orwhere (' name ', ' Academy ')->get ();
4, sorting
The Query Builder sorts query results by using the order by method:
5. Group
Groups are generally used to aggregate queries, and then we use the GroupBy method to group query results, such as we can count several articles under each category:
We can also use the having method to add conditions to the grouping, for example, we can count the categories with the total number of views greater than 500:
$posts = db::table (' posts ')->select (' cat_id ', Db::raw (' SUM (views) as views ')->groupby (' cat_id ')->having (' Views ', ' > ',->get ();
DD ($posts);
Note: The conditional field in the having must appear in the Select query field or it will be an error.
6, pagination
The Query Builder uses skip and take to page through the query results, which is equivalent to the limit statement in the SQL statement:
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.