If value not in one table query second table

I have a page that would pull two variables, q and vlgid from the URL string that is opened from a different page. i.e., image.php?q=911&vlgid=N829NW

Currently, my script would pull from just a single table where these two values are equal. However, I have a new table which is just for images.

What I’d like to know is if it’s possible to pull an image from the new table, if it exists and if it does not yet exist in this table, pull it from the other table and then display it on the page.

$lvmID = mysqli_real_escape_string($mysqli, $_GET['q']); 
$vlgID = mysqli_real_escape_string($mysqli, $_GET['vlgid']); 

SELECT r.*, lvm.luchtvaartmaatschappijID AS lvmID, lvm.IATACode
FROM tbl_random r
LEFT JOIN tbl_luchtvaartmaatschappij lvm
ON r.img_lvm = lvm.IATACode
WHERE lvm.lvmID = '$lvmID' AND r.img_nmr = '$vlgID'

The current page uses this query. I don’t like it, per se, because it ONLY has one result. Whereas, using the one above could result in many result possibilities.

$lvmID = mysqli_real_escape_string($mysqli, $_GET['q']); 
$vlgID = mysqli_real_escape_string($mysqli, $_GET['vlgid']); 

SELECT vlg.lvmID, lvm.luchtvaartmaatschappij, t.toestel, vlg.toestel, vlg.erlr, vlg.inschrijvingnmr, vlg.status,, vlg.ln, vlg.delivered, vlg.vliegtuignaam, vlg.became, vlg.vorigeLVMID, vlg.vorigeInschrijv, vlg.firstflight, vlg.engines, vlg.configuratie, vlg.remark, vlg.specialekleuren, vlg.fleetnmr, vlg.inactive, vlg.exitdate, vlg.beeld, lvm.logo, vlg.beeld_linkje, vlg.beeld_copyright, vlg.photouse_approve, vlg.photouse_approve_date, vlg.beeld_comment

FROM tbl_vliegtuiggegevens vlg

LEFT JOIN tbl_toestel t
ON vlg.toestelID = t.toestelID

LEFT JOIN tbl_luchtvaartmaatschappij lvm
ON vlg.lvmID = lvm.luchtvaartmaatschappijID

WHERE vlg.lvmID='$lvmID' AND vlg.inschrijvingnmr='$vlgID'AND vlg.photouse_approve IN ('GRANTED','APPROVED','WAITING','PENDING')
ORDER BY luchtvaartmaatschappij, t.toestel, vlg.inschrijvingnmr ASC;

I suppose that maybe a UNION query, could work? Though I’m not as familiar with doing those kind of queries.

If, there are no results from the first query, it should then look for a result in the second query and then I suppose I would echo results based on if/else? Something akin to this (which, for the moment, only uses the second query example

<?php if ($row[‘photouse_approve’] == ‘APPROVED’){ echo “<a href='” . $row[‘beeld_linkje’] . “’ target=‘_blank’><img src='” . $row[‘beeld’] . “’ width=‘900’></a>”; } else if($row[‘photouse_approve’] == ‘GRANTED’){ echo “<a href='” . $row[‘beeld_linkje’] . “’ target=‘_blank’><img src='” . $row[‘beeld’] . “’ width=‘900’></a>”; } else{ echo “<img src=‘photo_permission_pending.jpg’ width=‘900’>”; } ?>

What are more experienced users thoughts on this?

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi @cronk005

Without knowing your database structure or the application, I would probably just run two different queries and do the logic in PHP. One query asking the newTable and if result is empty, then query the oldTable.

You might get a better answer by asking in a MySQL specific forum: