查询表中的记录,其中attribute =来自先前查询的数组中的项

table1: id (autoincrement) username category

table2: id (autoincrement) itemname category

I have 2 queries.

1 query one shows categories that a user (logged in) belongs to:

 $sql = "SELECT * FROM table1 WHERE '$login_session' = username";

I can list each record:

 if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
      echo "category: " . $row["category"]. "<br>";
 }

That works fine.

My 2nd query is dependent on the 1st query. I need to list all records from table2 where category belongs to in array category in previous query.

 $sql2 = "SELECT * FROM table2 WHERE category = I DO NOT KNOW WHAT TO PUT HERE";

How do I write $sql2?

One method uses in:

SELECT *
FROM table2
WHERE category IN (SELECT category FROM table1 WHERE '$login_session' = username");

You can use IN()

$sql =  'SELECT * FROM table2 WHERE category IN ('"' . implode('","', $arrayOfCategories).'"')'