A
A
Alexey Nikolaev2015-09-25 13:50:33
PHP
Alexey Nikolaev, 2015-09-25 13:50:33

Why doesn't INNER JOIN work?

Good afternoon.
There are two tables - A and B. I'm trying to display all records from tables A and B that have the condition A.id = B.id and A.id_order is equal to a certain value. I wrote this query for this:

SELECT A.id, B.price 
                  FROM `table_A` as A
                  INNER JOIN `table_B` as B ON A.id = B.id
                  WHERE A.id_order = 100

Logically, everything should work, but for some reason it doesn't work, and I can't figure out why. No line is output. Remove WHERE - there will be too many rows. Since INNER JOIN requires both fields to be found from ON in the tables, you cannot move the condition from WHERE to ON. The value in the WHERE clause is correct.
Where could I have made a mistake?
I would be grateful for answers, thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2015-09-25
@Heian

it means that in table B there is no id that matches the id in table A, for which A.id_order=100
to check, do

SELECT DISTINCT(id) FROM table_A WHERE id_order=100

and compare it with the output SELECT DISTINCT(id) FROM table_В
well or in 1 query
SELECT * FROM table_В WHERE id IN (SELECT DISTINCT(id) FROM table_A WHERE id_order=100)
- should return an empty list here.
as an option - replace INNER JOIN with LEFT JOIN - you will see a bunch of А.id without a price

R
Rsa97, 2015-09-25
@Rsa97

So there are no tables `op` and `pr` in the query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question