K
K
Kratko632020-10-15 12:48:58
PHP
Kratko63, 2020-10-15 12:48:58

How to make a MySQL SELECT query from 2 tables based on multiple values?

Hello, there are 2 tables

1 table showing product id and product model

+----------+-----------+
|    id    |   model   |
+----------+-----------+
|   1001   |  Апельсин |
|   1002   |  Лимон    |
|   1003   |  Вишня    |
|   1004   |  Калина   |
+----------+-----------+

2 table indicating the product id (as in the first), product category, and another (3rd column) indicating Main 1 or not main 0
+----------+-----------+------------------+
|    id    |   category  |  main_category |
+----------+-----------+------------------+
|   1001   |      1      |        0       |
|   1001   |      2      |        0       |
|   1001   |      3      |        0       |
|   1002   |      1      |        0       |
|   1002   |      7      |        1       |
|   1003   |      1      |        0       |
|   1003   |      4      |        1       |
|   1003   |      8      |        0       |
|   1003   |      10     |        0       |
|   1004   |      1      |        0       |
|   1004   |      2      |        0       |
|   1004   |      5      |        0       |
+----------+-------------+----------------+


In the 3rd column there may be a product for which the main category is not set, in the example it is a product with id 1001 and 1004

Please tell me how to display a list of all products (with data from the first and second tables) that do NOT have the main category in one query category? Those. if you look at the example, then display data with id 1001 and 1004

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-03-02
@unitby

Does form.php exactly see the named constants you defined?

I
idShura, 2020-10-15
@idShura

select t1.id,
       t1.model,
       t2.category,
       t2.main_category
  from table1 t1
       left join table2 t2 on t2.id = t1.id
 where t2.main_category = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question