Jump to content

Finding One Row In A Couple Tables


nodirtyrockstar

Recommended Posts

I am a beginner with MySQL and was wondering what method advanced users would recommend to perform the simple task of looking through two tables to find one row. I don't need code from you, just a general idea of what type of query is best for this situation. If you can offer an explanation of why you like the method, that's even better.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/272133-finding-one-row-in-a-couple-tables/
Share on other sites

First of all, you should be explicit with your join.

 

A join would look like this:

SELECT t1.fields, t2.fields 
FROM t1
LEFT (or INNER) JOIN t2 
ON t1.fk = t2.pk
WHERE t1.field = 'value';

 

However, you need to explain your goal a little better. " looking through two tables to find one row" doesn't make much sense.

Actually, I have another question related to this. At runtime, my script actually has the table value stored in a PHP variable. Is it possible to use bind_params when preparing the query to plug in the table name?

 

$stmt = $mysqli->prepare("SELECT * FROM ? WHERE `id` = ?;");
$stmt->bind_param('ss', $tbl, $id);

 

I tried the code above and it isn't working.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.