[英]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
. 我有两个表,
services
和services_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_id
和service_id
是PRIMARY KEY
。
here ser_cat_id
save into scid
in the services
table. 这里
ser_cat_id
保存到scid
的services
表。 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_id
从services_cat
表。
I can pass ser_cat_id
to scid
from service_cat
table and I can fetch (display) that scid
. 我可以通过
ser_cat_id
到scid
从service_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
(相同scid
的services
表)。
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.