0

If i have 2 queries like this

$query1 = "SELECT title, content, image FROM table1 WHERE id = :id";
$query2 = "SELECT title, content FROM table2 WHERE id = :id";

Can i prepare two of them at once like something like this

$stmt = $conn->prepare($query1, $query2);
$stmt->execute([':id' => $id]);
Calibur Victorious
  • 638
  • 1
  • 6
  • 20

1 Answers1

0

You can do this with MySQL UNION.

Sanan Guliyev
  • 711
  • 5
  • 11
  • The columns numbers isn't matched. – Calibur Victorious Jun 15 '17 at 11:54
  • It is not problem. In second query you can do like this: SELECT '' AS image – Sanan Guliyev Jun 15 '17 at 11:56
  • Didn't catch it correctly, so in the 2nd query i'll make it like this? `$query = "SELECT title, content, image FROM table1 WHERE id = :id UNION ALL SELECT title, content, image AS image FROM table2 WHERE id = :id ";` or something like that? If possible put an example using my example – Calibur Victorious Jun 15 '17 at 11:58
  • You can use it like that: `SELECT title, content, image FROM table1 WHERE id = :id UNION ALL SELECT title, content, '' AS image FROM table2 WHERE id = :id` – Sanan Guliyev May 03 '18 at 20:05