In THINKPHP, you can use the table () method or the connection method for association query (multi-table query). See the example.
In THINKPHP, you can use the table () method or the connection method for association query (multi-table query). See the example below:
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)
-> Where ('status> 1 ')
-> Select ();
Model-> Table (think_blog blog, think_type)
-> Where ('Blog. typeid = type. id ')
-> Field ('Blog. id is ID, blog. content, type. typename type of blog. title ')
-> Order ('Blog. id DESC ')
-> Limits (5)
-> Select ();
Parameters of the table method support strings and arrays. use the array method:
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 ')
-> Connection (U. id = N. cid on news N)
-> Field ('U. * N .*')
-> Command (id desc ')
-> Limit ('8 ')
-> Findall ();
The left join method is used by default. if you need other joining methods, you can change it
Model-> JOIN (right join works ON artist. id = work. artist_id ')
-> Select ();
// RIGHT JOIN
Model-> Table ('User u ')
-> Join (array ('right', 'news n u. id = N. cid ))
-> Field (U. * N .*')
-> Command (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 joining (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. How to View SQL statements of a coherent operation:
Echo $ model-> getLastSql () // Print the SQL statement and check it
Example 2:
1. table ()
$ List = $ ('User _ status statistics, user_profile data) user> Table> where ('stats. id = profile. typeid')-> Field ('Display, profile as ID, stats. display stats. id. Title as the title, content profile. content ")-> command ('stats. id DESC ')-> Select ();
2.1, add (2) table query
$ User = new mode ("user ");
$ List = $ user-> JOIN ('right JOIN user_profile's ON user_stats.id = user_profile.typeid ');
2.2, add () multi-table query
List = $ Table-> connection ('On think_form.sort_id = think_sort.sort_id think_sort ")-> connection ('On think_form.brand_id = think_brand.brand_id think_brand")-> Select ();
3. native query
Model new model ();
$ SQL = 'SELECT a. id, a. title, and use think_test1 B. content as the $ Map "where think_test2 is B a. id = B. ID'. For a. ID '. Sort. 'Limited' $ P-> firstRow. ',' $ P-> ListRows is set
$ VoList = $ model-> query (SQL );