Table method: defines the name of the data Table to be operated. you can dynamically change the name of the data Table for the current operation. you need to write the full name of the data Table, including the prefix, and use the alias.
1. Table method: defines the name of the data Table to be operated. you can dynamically change the name of the data Table for the current operation. you need to write the full name of the data Table, including the prefix, and use an alias, for example:
$ Model-> Table ('think _ user user ')
-> Where ('status> 1 ')
-> Select ();
$ Model-> table ('think _ blog, think_type type ')
-> Where ('Blog. typeid = type. id ')
-> Field ('Blog. id as id, blog. title, blog. content, type. typename as type ')
-> Order ('Blog. id desc ')
-> Limit (5)
-> Select ();
The parameters of the Table method support strings and arrays. the usage of the array method is as follows:
$ Model-> Table (array ('think _ user' => 'user', 'think _ group' => 'Group '))
-> Where ('status> 1 ')
-> Select ();
The advantage of using arrays to define is to avoid errors due to table name and keyword conflicts.
Note: If the table method is not defined, the data table corresponding to or defined in the current model is automatically obtained by default.
2. Join method: query Join is supported. Join parameters support strings and arrays. join is the only method that can be called multiple times in a coherent operation. For example:
$ Model-> join ('Work ON artist. id = work. artist_id ')
-> Join ('card ON artist. card_id = card. id ')
-> Select ();
// Left Join
$ Model-> table ('User u ')
-> Join ('news N on U. id = N. Cid ')
-> Field ('U. *, N .*')
-> Order ('Id desc ')
-> Limit ('8 ')
-> Findall ();
The left join method is used by default. if you want to use another JOIN method, you can change it
$ Model-> join ('right JOIN work ON artist. id = work. artist_id ')
-> Select ();
// Right Join
$ Model-> table ('User u ')
-> Join (array ('right', 'news N on U. id = N. Cid '))
-> Field ('U. *, N .*')
-> Order ('Id desc ')
-> Limit ('8 ')
-> Findall ();
If the parameters of the join method use arrays, the join method can only be used once and cannot be used together with the string method.
$ Model-> join (array ('Work ON artist. id = work. artist_id ', 'card ON artist. card_id = card. id '))
-> Select ()
This consistent operation method can effectively improve the code definition and development efficiency of data query.
Knowledge supplement:
How to View SQL statements of a coherent operation:
Echo $ Model-> getLastSql (); // Print the SQL statement and check it.