简体   繁体   English

当我从另一个表中存储其ID时如何显示字段值

[英]how to show field value when i store its id from another table in mysql

I have two tables, holiday_package and main_attractions . 我有两个表holiday_packagemain_attractions

holiday_package contains the field package_title and main_attractions contains package_title and main_attraction fields. holiday_package包含字段package_title, main_attractions包含package_title和main_attraction字段。 I am inserting package_title id from holiday_package table to main_attractions table field package_title. 我正在将holiday_package表中的package_title ID插入main_attractions表字段package_title中。

I want to show package_title and main_attraction data from main_attractions table to administrator in admin panel, so instead of package_title id I want to show its value. 我想在管理面板中将main_attractions表中的package_title和main_attraction数据显示给管理员,因此我想显示其值而不是package_title id。 How can I write a SQL query for that? 如何为此编写SQL查询? Please help me! 请帮我!

My php code: 我的PHP代码:

$sql = "select * from main_attractions";
      $i = 1;

    $res = mysql_query($sql, $conn); 

    if( mysql_num_rows($res) > 0) {
        while($row = mysql_fetch_array($res))
     {

    echo "<tr>
      <td>".$i++."</td>
      <td>".$row["package_title"]."</td>
      <td>".$row["main_attraction"]."</td>
      <td><a href='edit-main-attraction-details.php?id=$row[id]'>Edit</a></td>
      <td><a href='delete-main-attraction.php?id=$row[id]' onclick='return confirmSubmit();'>Delete</a></td>
    </tr>";
     }
    }

With join you can do it like this 通过加入,您可以像这样

holiday_package holiday_package

 package_title  package_id
_____________  _____________
    A                  1
    B                  2
    c                  3

main_attractions main_attractions

 main_attraction    package_title
_____________     _____________
    A1                    1
    B1                    2
    c1                    3

So 所以

select holiday_package.package_title,main_attraction from main_attractions 
left join holiday_package on main_attractions.package_title=holiday_package.package_id
select * from main_attractions left join holiday_package on main_attractions.package_title = holiday_package.package_id

可以用*代替*来获取要提取的字段名称

暂无
暂无

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

相关问题 mysql查询-仅从另一个表中选择id = id,而该表中的字段=值 - mysql query - only select where id = id from another table and a field in that table = a value MySQL-如何在另一个表的逗号分隔字段中选择ID值所在的表中的行? - MySQL - How to select rows in a table where id value is in a comma delimited field in another table? 将关联值从一个表存储到mysql中的另一个表 - Store associated value from one table to another table in mysql 用另一个表中的名称替换拉出的SQL ID值 - Replacing a pulled SQL ID value with its name from another table MySQL-从另一个表中查询与当前表相同的ID的字段 - mySQL - Querying a field from another table with the same id as in the current one 如何借助mysql表中的id值回显其他字段(列)值 - How to echo other field (column) value with the help of id value from mysql table 如何从单个字段中查找超时和超时并将其值提取到SQL中的另一个表 - How to find time-in and time-out from a single field and extract its value to another table in SQL 如何使用php mysql将行ID插入另一个表字段? - How to insert row id to another table field using php mysql? 如何将值存储在从mysql表检索的变量中,以及如何在使用php的另一个查询中使用它 - how to store the value in a variable retrieved from mysql table and to use it in another query using php mysql-通过id从一个表中选择一个产品,然后从另一个表中选择其关联的图像 - mysql- select a product from one table by id, then its associated images from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM