[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP Comparing values in SQL tables

Posted on 2010-11-30
3
Medium Priority
?
227 Views
Last Modified: 2012-05-10

I have two tables (A & B). I want to list on my PHP page all the values from table A and a Yes next to each record if there is a matching record in table B.

Suppose we have the following:
Table A      Support Categories                  
| ID      | Description                      |
| 1      | CV Writing Assistance    |
| 2      | Job Search Assistance   |
| 3      | Interview Skills                |

Table B      
| ClientID  |      SupportID  (references table A  ID)  |      SupportStarted     |      SupportStopped
| C1         |      1                                                       |      01/06/10              |            01/06/10
| C2         |      1                                          |      01/06/10      
| C1         |        2                                          |      05/07/10             |             06/07/10
| C1         |      2                                          |       09/09/10      
| C4         |      1                                          |       19/08/10      

I want to show for a given client whether they have started to receive support and whether they have any opened support areas  (not stopped) in my PHP page.

For example Example.php
---------------------------------------------------------------------------------------------------------
CLIENT ID 1

Description                           |     Support Started       |     Open Support

CV Writing Assistance      |           Yes                            |          No
Job Search Assistance      |           Yes                            |         Yes
Interview Skills      |             No                            |          No

_____________________________________________________________________________

Normally I would do the following.
1.      Run a query which returns all rows from Table A
2.      Use a While() loop to display each row from table A.
3.      Within the While() loop query each record from table B to look for matching rows.
See attached code.
I was wondering if there was a better (more efficient) way of doing this without having to repeat the second query each time a row is loaded from the first query?
Is there any way to compare query results whilst they are in the array?

Example.php
0
Comment
Question by:EICT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Accepted Solution

by:
darren-w- earned 600 total points
ID: 34239641
Might be better doing this in sql

something like

select ClientID, description, 'yes' as Support_started, 'yes' as 'open_support'
from table_b b
left join table_a a
ON b.SupportID = a.ID
where b.ClientID = c1

then just printing this output

there will need to be a little more logic applied to display the support statuses.

D
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 34239692
There are some things not worth optimizing, and this might be one of them.  If you do not have thousands of rows, and your data base is well-indexed, you might be OK with almost any query structure!
0
 

Author Closing Comment

by:EICT
ID: 34270144
The query was wrong but the principle is there. Try to do as much in mysql first to you only have to run the query once.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question