[英]SQL query using two or more tables
I am trying to get information from my SQL into PHP page, the problem I have I am trying to use multiple tables to populate the result 我试图从我的SQL获取信息到PHP页面,我有的问题我试图使用多个表来填充结果
Table's columns are same names though but table names obviously differs. 表的列虽然名称相同,但表名明显不同。
Table ticket 表票
status_id; STATUS_ID; ticket_id
TICKET_ID
Table ticket__cdata 表ticket__cdata
ticket_id TICKET_ID
SELECT * FROM ticket WHERE status_id = '1' ORDER BY created DESC
I filter the table only looking for 1 in TABLE A 我过滤表只查找表A中的1
I echo the TICKET_ID on the php from Table A but now I want to filter in Table B the ticket_id and pull information from that form. 我在表A中回显了PHP上的TICKET_ID,但现在我想在表B中过滤ticket_id并从该表单中提取信息。 Table be does not have status_id in it so cannot filter on that again.
表中没有status_id,因此无法再次对其进行过滤。
Logic says I must say something like this 逻辑说我必须说出这样的话
SELECT * FROM ticket_cdata WHERE ticket_id = '".$ticket_id."'
Here is my code to date without the second query 这是我的代码到目前为止没有第二个查询
$result = mysql_query("SELECT * FROM ost_ticket WHERE status_id = '1' ORDER
BY created DESC");
?>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td class="center">
<div align="center"><? echo $rows['number']; ?></div></td>
<td><div align="center"><? echo $rows['ticket_id']; ?></div></td>
<td><div align="center"><? echo $rows['lbs_time']; ?></div></td> //This it the info required from TABLE ticket but must be linked to the status_id
UPDATED STRING 更新的字符串
$result = mysql_query("SELECT tc.*
FROM ticket_cdata tc
JOIN ticket t ON tc.ticket_id = t.ticket_id
WHERE t.status_id = '1'
ORDER BY t.created DESC");
?>
<tr>
<td class="center">
<div align="center"><? echo $rows['number']; ?></div></td>
<td><div align="center"><? echo $rows['ticket_id']; ?></div></td>
<td><div align="center"><? echo $rows['created']; ?></div></td>
<td><div align="center"><? echo $rows['subject']; ?></div></td>
Now I cannot Echo any info 现在我无法回应任何信息
You could use a join: 您可以使用联接:
SELECT tc.*
FROM ticket_cdata tc
JOIN ticket t ON tc.ticket_id = t.ticket_id
WHERE t.status_id = '1'
ORDER BY t.created DESC
Your initial query can be changed to this: 您的初始查询可以更改为:
SELECT * FROM ticket t
WHERE t.status_id = '1'
AND EXISTS (SELECT * FROM ticket__cdata WHERE ticket_id = t.ticket_id)
ORDER BY t.created DESC
This limits results to tickets that are present in ticket__cdata
table and have status_id = '1'
. 这会将结果限制为
ticket__cdata
表中存在的票证,并且status_id = '1'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.