Jump to content

PHP Help needed


Dealmightyera

Recommended Posts

I would use a single query which checks if the code is used in one table or the other, or both.

Test data

TABLE: tsc                              TABLE: candidates
+----+-----------------+                +---------+-------+--------+
| id | transactioncode |                | regcode | name  | last   |
+----+-----------------+                +---------+-------+--------+
| 10 |               1 |                |       2 | Alma  | Geddon |
| 11 |               2 |                |       4 | Scott | Chegg  |
| 12 |               3 |                |       5 | Sarah | Tonin  |
| 13 |               4 |                +---------+-------+--------+
+----+-----------------+

The query and results

mysql> SELECT a.transactioncode as tsc
    ->      , coalesce(b.regcode, 'NO') as candidates
    -> FROM tsc a
    ->      LEFT JOIN
    ->      candidates b ON a.transactioncode = b.regcode
    -> UNION
    -> SELECT coalesce(a.transactioncode, 'NO') as tsc
    ->      , b.regcode as candidates
    -> FROM candidates b
    ->      LEFT JOIN
    ->      tsc a ON a.transactioncode = b.regcode;
+------+------------+
| TSC  | CANDIDATES |
+------+------------+
| 1    | NO         |
| 2    | 2          |
| 3    | NO         |
| 4    | 4          |
| NO   | 5          |
+------+------------+

Because you are interested in one code at a time, just add WHERE clauses to each half of the UNION. I.E....

SELECT a.transactioncode as TSC
     , coalesce(b.regcode, 'NO') as CANDIDATES
FROM tsc a 
     LEFT JOIN 
     candidates b ON a.transactioncode = b.regcode
WHERE transactioncode = ?
UNION 
SELECT coalesce(a.transactioncode, 'NO') as TSC
     , b.regcode as CANDIDATES
FROM candidates b
     LEFT JOIN 
	 tsc a ON a.transactioncode = b.regcode;
WHERE regcode = ?

Note that the the inputted code is used twice, so your execute command would be

$stmt->execute( [ $code, $code ] );

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.