SQL query using two or more tables

I am trying to get information from my SQL to a PHP page, the problem I have is I am trying to use multiple tables to populate the result

The columns of the columns have the same names, although the table names are obviously different.

Table ticket

status_id; TICKET_ID

Ticket__cdata table p>

TICKET_ID

 SELECT * FROM ticket WHERE status_id = '1' ORDER BY created DESC

      

I am filtering a table for only lookup 1 in table A

I am repeating TICKET_ID to php from table A, but now I want to filter ticket_id on table B and extract information from this form. Table be has no status_id so cannot filter on this again.

Logic says I should say something like this

 SELECT * FROM ticket_cdata WHERE ticket_id = '".$ticket_id."' 

      

Here is my code as of today without the second request

$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 can't Echo any info

+3


source to share


2 answers


You can use a connection:



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

      

+2


source


The original query can be modified as follows:

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 the results that are present in the table ticket__cdata

and have status_id = '1'

.

0


source







All Articles