简体   繁体   中英

Sql query from one table and to query another table with result from first query table

No sure if the title makes sense in what i'm trying to achieve.

I have 2 tables one called customer_details and the other repair_details. I am querying the database to load some info from repair_details using the do until rs.EOF and inside of this I want to query another table using one of the fields to report if the user has paid:

My code is like this (in vbscript)

<%
strSQL = "SELECT * FROM repair_details WHERE repair_progress = 0 ORDER BY id asc"               
Set rs = conn.Execute(strSQL)

   If rs.EOF Then
     booking_code ="None"
     serial = "None"
     fault = "None"
     status = "None"
   Else
   do until rs.EOF
     ID = rs("cust_id")
     booking_code = rs("booking_code")
     serial = rs("serial")
     fault = rs("fault")
     status = rs("status")
 %>             
<tbody>
 ..... rest of my page

Now inside of customer_details I have a column called cust_id and also another column called pay_needed.

I want to run the first query (repair_details) and whilst it's running this, to look inside of customer_details and if under pay_needed it reports yes then for this to be stored as payment_needed. Lower down in my coding ive written this to highlight who needs to pay:

If payment_needed = "Yes" Then%>
  <td nowrap><font color=#FF0000><%=booking_code%></font></td>                              
<%Else%>        
  <td nowrap><%=booking_code%></td> 
<%End If%>

This is what I've written and of course it doesn't work and I know there's an easier way but it beyond on knowledge.

<%
strSQL = "SELECT * FROM repair_details WHERE repair_progress = 0 ORDER BY id asc"               
Set rs = conn.Execute(strSQL)

 If rs.EOF Then
  booking_code ="None"
  serial = "None"
  fault = "None"
  status = "None"
 Else
 do until rs.EOF
  ID = rs("cust_id")
  booking_code = rs("booking_code")
  serial = rs("serial")
  fault = rs("fault")
  status = rs("status")
 %>             
 <tbody>
 <tr>
 <%
   strSQL1 = "SELECT payment_needed FROM customer_details WHERE cust_id = '" & ID & "'"                 
   Set rs = conn.Execute(strSQL1)

    payment_needed = rs("payment_needed")

    If payment_needed = "Yes" Then%>
     <td nowrap<font color=#FF0000><%=booking_code%></font></td>        <%Else%>        
     <td nowrap><%=booking_code%></td>  
    <%End If%>  
   </tr>
   </tbody>
   <%   
    rs.MoveNext
    loop
   End if
   rs.Close
   %>

Thanks in advance

Try this query:

SELECT RD.cust_id, RD.booking_code, RD.serial, RD.fault, RD.[status], CD.payment_needed
FROM repair_details RD
INNER JOIN customer_details CD ON CD.cust_id = RD.cust_id

It will do what you do in the loop for all the repair_details' fields. You will not need the second query (strSQL1) anymore.

Then you just need to read everything from there:

ID = rs("cust_id")
booking_code = rs("booking_code")
serial = rs("serial")
fault = rs("fault")
status = rs("status")
payment_needed = rs("payment_needed")

If you want to show all rows from repair_details, regardless of having a valid cust_id:

SELECT RD.cust_id, RD.booking_code, RD.serial, RD.fault, RD.[status], COALESCE(CD.payment_needed,'') AS payment_needed
FROM repair_details RD
LEFT OUTER JOIN customer_details CD ON CD.cust_id = RD.cust_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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