简体   繁体   English

如何用3表创建内部联接

[英]How to create inner join with 3 table

mysql> SELECT * FROM main_table;
+--------+----------+--------+
| id_book | id_author      |description |
+--------+----------+--------+
| 1           | 101     |   I love cat |
+--------+----------+--------+

mysql> SELECT * FROM author;
+---------+-----------+
|  id_author | name_author |
+---------+-----------+
| 101     | Dr Sent     |
+---------+-----------+

mysql> SELECT * FROM book;
+--------+---------+
| id_book | name_book |
+--------+---------+
|      1 |     cat |
+--------+---------+

hello I am still new in PHP now im bit confuse with PHP and MySQL. 你好,我对PHP还是很陌生,现在与PHP和MySQL有点混淆了。 I have a drop drown list which is related to one of my table in database. 我有一个淹死列表,它与数据库中的一个表相关。

firstly my system appear a page of list author's name and user will able to choose which one user like. 首先,我的系统出现一个列出作者姓名的页面,用户可以选择喜欢的用户。

 JK ROWLING 
 DR SEUSS <-- author_name
 ROAD DAHL

Next, it will go to a new page and have a select(list/menu) which show a list of BOOK NAME. 接下来,它将进入一个新页面并具有一个选择(列表/菜单),其中显示了书名列表。 I can retrieve the dropdown list from database, but my problem is when I select one of BOOK NAME in the dropdown, it show all book description and cannot select the right description. 我可以从数据库中检索下拉列表,但是我的问题是,当我在下拉列表中选择“书名”之一时,它会显示所有书的描述,而无法选择正确的描述。

here is my coding to make it more clear 这是我的编码,以使其更清楚

$currentPage = $_SERVER["PHP_SELF"];

mysql_select_db($database_config, $config);
$query_Recordset1 = "SELECT * FROM book ORDER BY name_book ASC";
$Recordset1 = mysql_query($query_Recordset1, $config) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$maxRows_Recordset2 = 1;
$pageNum_Recordset2 = 0;
if (isset($_GET['pageNum_Recordset2'])) {
  $pageNum_Recordset2 = $_GET['pageNum_Recordset2'];
}
$startRow_Recordset2 = $pageNum_Recordset2 * $maxRows_Recordset2;

$colname_Recordset2 = "-1";
if (isset($_GET['id_book'])) {
  $colname_Recordset2 = $_GET['id_book'];
}
mysql_select_db($database_config, $config);
$query_Recordset2 = sprintf("SELECT * FROM main_table WHERE id_book = %s ORDER BY description ASC", GetSQLValueString($colname_Recordset2, "int"));
$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);
$Recordset2 = mysql_query($query_limit_Recordset2, $config) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);

if (isset($_GET['totalRows_Recordset2'])) {
  $totalRows_Recordset2 = $_GET['totalRows_Recordset2'];
} else {
  $all_Recordset2 = mysql_query($query_Recordset2);
  $totalRows_Recordset2 = mysql_num_rows($all_Recordset2);
}
$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;

$queryString_Recordset2 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset2") == false && 
        stristr($param, "totalRows_Recordset2") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset2 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset2 = sprintf("&totalRows_Recordset2=%d%s", $totalRows_Recordset2, $queryString_Recordset2);

Try This......... 尝试这个.........

SELECT 
    a.*, b.*, c.* 
FROM main_table a 
    inner join author b on a.id_author = b.id_author 
    inner join book c on a.id_book = c.id_book

First off, I'd recommend using AJAX and staying on the same page when an item is selected. 首先,我建议使用AJAX并在选择一个项目时保持在同一页面上。 By this you can separate the code for the different input boxes and it'll look better at the end of the day. 这样,您可以将不同输入框的代码分开,到最后看起来会更好。

To answer your question, here's the SQL Statement for mySQL you should run to get what you asked for: 要回答您的问题,以下是mySQL的SQL语句,您应该运行该命令以获取所需的内容:

SELECT MAT.*,AUT.name_author, BOO.name_book
FROM main_table AS MAT, author AS AUT, book AS BOO
WHERE (MAT.id_book=BOO.id_book AND MAT.id_author=AUT.id_author) AND MAT.id_author=101
ORDER BY MAT.id_author, MAT.id_book;
You can find the SQL Fiddle code for the above guy here . 您可以在此处找到上述人员的SQL Fiddle代码。 You can update the "filtering" in the WHERE clause to filter by another table's author ID or by book ID 您可以更新WHERE子句中的“过滤”以按另一个表的作者ID或书籍ID进行过滤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM