简体   繁体   English

如何将值从另一个表获取到PHP / MySQL中的另一个表

[英]How to Get VALUES from another table into another table in PHP/MySQL

First I should say my English is not perfect. 首先,我应该说我的英语并不完美。 I am using XAMPP 5.6.0.0 我正在使用XAMPP 5.6.0.0

I have two tables, services and services_cat . 我有两个表, servicesservices_cat

    -- Table structure for table `services_cat`

    CREATE TABLE IF NOT EXISTS `services_cat` 
   (`ser_cat_id` int(11) NOT NULL,
    `service_category` varchar(50) NOT NULL)

    -- Table structure for table `services`

    CREATE TABLE IF NOT EXISTS `services` 
    (`service_id` int(11) NOT NULL,
     `scid` int(11) NOT NULL,
     `service_title` varchar(50) NOT NULL,
     `service_statement` varchar(1000) NOT NULL,
     `service_photo` varchar(100) NOT NULL)

I want to show following data in one table. 我想在一张表中显示以下数据。

service_id
ser_cat_id
service_category
service_title
service_statement

ser_cat_id and service_id are PRIMARY KEY s. ser_cat_idservice_idPRIMARY KEY

here ser_cat_id save into scid in the services table. 这里ser_cat_id保存到scidservices表。 This ser_cat_id save multiple times in the scid colomn. ser_cat_id在节省多次scid colomn。 But service_title will be changed. 但是service_title将被更改。 There are different types of service_title , but the scid is same for that different service_title s. 有不同类型的service_title ,但scid是相同的,不同的service_title秒。 That scid comming from ser_cat_id from services_cat table. scid从正在添加ser_cat_idservices_cat表。

I can pass ser_cat_id to scid from service_cat table and I can fetch (display) that scid . 我可以通过ser_cat_idscidservice_cat表,我可以取(显示器) scid But I want to display that DISTINCT service_category name of the ser_cat_id (same as scid in services table). 但我想,以显示DISTINCT service_category的名称ser_cat_id (相同scidservices表)。

Can you please help me .. 你能帮我么 ..

Following is my code. 以下是我的代码。

    <?php
    include_once("conn.php");

    $sql = mysql_query("SELECT * FROM services_cat JOIN services ON services_cat.ser_cat_id = services.service_id GROUP BY service_category ");
    while($row = mysql_fetch_object($sql))
    {

        echo "<tr>
        <td><p class='table-p'>$row->scid</p></td>
        <td><p class='table-p'>$row->ser_cat_id</p></td>
        <td><p class='table-p'>$row->service_category = $row->scid</p></td>
        <td><p class='table-p'>$row->service_title</p></td>
        <td><p class='table-p'>$row->service_statement</p></td>

        <td><p class='table-p'><a href='service_edit.php?ide=$row->service_id'>Edit</a> |
        <a href='service_delete.php?idd=$row->service_id'>Delete</a></p></td>
        ";
    }
    ?>

Based on what you want, this is what you need: 根据您的需要,这是您需要的:

SELECT s.service_id, c.ser_cat_id, c.service_category, s.service_title, s.service_statement 
FROM services_cat AS c 
INNER JOIN services AS s 
  ON c.ser_cat_id = s.scid 

Check this running example: http://sqlfiddle.com/#!9/013f7/3/0 检查此正在运行的示例: http : //sqlfiddle.com/#!9/013f7/3/0

Inside your loop, your $row will have this data, as you need. 在循环中,您的$row将根据需要具有此数据。

$row['service_id']
$row['ser_cat_id']
$row['service_category']
$row['service_title']
$row['service_statement']

try to change, 尝试改变

$sql = mysql_query("SELECT * FROM services_cat JOIN services ON services_cat.ser_cat_id = services.service_id GROUP BY service_category ");

to

$sql = mysql_query("SELECT * FROM services_cat JOIN services ON services_cat.ser_cat_id = services.scid GROUP BY service_category ");

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

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