What is INNER JOIN in MySQL?In MySQL the INNER JOIN selects all rows from both participating tables to appear in the result if and only if both tables meet the conditions specified in the ON clause. JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents. In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise. Show Pictorial presentation of MySQL INNER JOIN : MySQL INNER JOIN Syntax: MySQL supports the following JOIN syntaxes for the table_references (A table reference is also known as a join expression.) part of SELECT statements and multiple-table UPDATE and DELETE statements: table_references: escaped_table_reference [, escaped_table_reference] ... escaped_table_reference: table_reference | { OJ table_reference } table_reference: table_factor | join_table table_factor: tbl_name [PARTITION (partition_names)] [[AS] alias] [index_hint_list] | table_subquery [AS] alias | ( table_references ) join_table: table_reference [INNER | CROSS] JOIN table_factor [join_condition] | table_reference STRAIGHT_JOIN table_factor | table_reference STRAIGHT_JOIN table_factor ON conditional_expr | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor join_condition: ON conditional_expr | USING (column_list) index_hint_list: index_hint [, index_hint] ... index_hint: USE {INDEX|KEY} [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list]) | IGNORE {INDEX|KEY} [FOR {JOIN|ORDER BY|GROUP BY}] (index_list) | FORCE {INDEX|KEY} [FOR {JOIN|ORDER BY|GROUP BY}] (index_list) index_list: index_name [, index_name] ... Example : MySQL INNER JOIN When combining records from more than one tables, an user needs to indicate how records in a table can be matched to records in the other. As the both of tables have a cate_id column, we can match using that column. The ON clause is used to match records in two tables, based on the value of cate_id column. Usage of INNER JOIN combines the tables. An INNER JOIN allows rows from either table to appear in the result if and only if both tables meet the conditions specified in the ON clause. In this example, the ON clause specifies that the cate_id column of both book_mast and category table must match. If a cate_id does not appear in both of the tables, the row will not appear in the result because the condition in the ON clause fails. Only those categories will participate in the JOIN whose books are written in ENGLISH. Code:
Relational Algebra Expression: Relational Algebra Tree: Sample table: book_mast Sample table: category Sample Output: mysql> SELECT book_mast.book_id,book_mast.book_name,cate_descrip -> FROM book_mast -> INNER JOIN category -> ON book_mast.cate_id=category.cate_id -> WHERE book_mast.pub_lang="English"; +---------+-------------------------------------+--------------+ | book_id | book_name | cate_descrip | +---------+-------------------------------------+--------------+ | BK001 | Introduction to Electrodynamics | Science | | BK002 | Understanding of Steel Construction | Technology | | BK004 | Transfer of Heat and Mass | Technology | | BK010 | Fundamentals of Thermodynamics | Technology | | BK012 | The Nature of World | Nature | | BK009 | Mental Health Nursing | Medical | +---------+-------------------------------------+--------------+ 6 rows in set (0.04 sec) PHP script:
View the example in browser Example : MySQL INNER JOIN with alias The following MySQL statement returns book ids, the name of the the book, publisher's id, category description and publisher's language, where publisher's language is English and publisher's id is not equal to P004. Notice that aliases have been used to refer the column names. An INNER JOIN is performed based upon the condition that a category id in book_mast table must exist in category table also. Code:
Relational Algebra Expression: Relational Algebra Tree: Sample table: book_mast Sample Output: mysql> SELECT bk.book_id,bk.book_name,bk.pub_id,ca.cate_descrip,bk.pub_lang -> FROM book_mast AS bk -> INNER JOIN category AS ca ON bk.cate_id=ca.cate_id AND -> bk.pub_lang='English' AND bk.pub_id<>'P004'; +---------+-------------------------------------+--------+--------------+----------+ | book_id | book_name | pub_id | cate_descrip | pub_lang | +---------+-------------------------------------+--------+--------------+----------+ | BK001 | Introduction to Electrodynamics | P003 | Science | English | | BK002 | Understanding of Steel Construction | P001 | Technology | English | | BK010 | Fundamentals of Thermodynamics | P007 | Technology | English | | BK012 | The Nature of World | P008 | Nature | English | | BK009 | Mental Health Nursing | P007 | Medical | English | +---------+-------------------------------------+--------+--------------+----------+ 5 rows in set (0.00 sec) MySQL INNER JOIN using three tables Sample tables: table - doctors +-------+---------+ | docid | dname | +-------+---------+ | 1 | A.VARMA | | 2 | D.GOMES | +-------+---------+ table - specialize +------+----------+-------+ | spid | desc | docid | +------+----------+-------+ | 1 | special1 | 1 | | 2 | special2 | 2 | +------+----------+-------+ table - timeschedule +-----+------+----------+-------+ | tid | tday | sit_time | docid | +-----+------+----------+-------+ | 1 | MON | 17:00:00 | 1 | | 2 | WED | 08:00:00 | 1 | | 3 | TUE | 16:00:00 | 2 | | 4 | FRI | 09:00:00 | 2 | +-----+------+----------+-------+ The above tables are related to each other. In doctors, specialize and timeschedule tables the docid, spid and tid are primary key consecutively. The docid in specialize table and timeschedule tables are a foreign key, which is the reference to primary key docid of doctors table. If we want all records for a doctor who are specialized in special1 and seat in his chamber on Wednesday (WED) in his schedule time, the following SQL can be used- Code:
Sample Output: +-------+---------+----------+------+----------+ | docid | dname | desc | tday | sit_time | +-------+---------+----------+------+----------+ | 1 | A.VARMA | special1 | WED | 08:00:00 | +-------+---------+----------+------+----------+ Explanation : Step-1 SELECT a.docid,a.dname,b.desc FROM doctors a INNER JOIN specialize b ON a.docid=b.docid; Step-2 SELECT a.docid,a.dname, b.desc,c.tday,c.sit_time FROM doctors a INNER JOIN specialize b ON a.docid=b.docid INNER JOIN timeschedule c ON a.docid=c.docid; Step-3 SELECT a.docid,a.dname, b.desc,c.tday,c.sit_time FROM doctors a INNER JOIN specialize b ON a.docid=b.docid INNER JOIN timeschedule c ON a.docid=c.docid WHERE a.docid=1 AND c.tday='WED'; Key points to remember Click on the following to get the slides presentation - INNER JOINS: SQL and other Relational Databases
Previous: Select
with date_add() Apa itu inner join mysql?Kesimpulannya, Inner Join adalah suatu perintah pada SQL Server digunakan untuk menggabungkan beberapa tabel . Sehingga, akan menampilkan data-data yang mempunyai nilai sama antar tabel dalam sebuah database.
Jelaskan apa yang dimaksud dengan fungsi inner join?Inner join menggabungkan data dari dua tabel kapan pun ditemukan nilai mirip dalam bidang umum di kedua tabel.
Apa itu Inner join dan Left join?INNER JOIN: mengembalikan baris ketika ada kecocokan di kedua tabel. LEFT JOIN: mengembalikan semua baris dari tabel kiri, bahkan jika tidak ada kecocokan di tabel kanan. RIGHT JOIN: mengembalikan semua baris dari tabel kanan, bahkan jika tidak ada kecocokan di tabel kiri.
Apa itu join dalam SQL?JOIN merupakan salah satu fungsi yang ada di SQL yang digunakan untuk penggabungan table melalui kolom atau key tertentu dimana memiliki nilai terkait untuk mendapatkan satu set data dengan informasi lengkap.
|