E
E
Evgeny Kramor2018-05-16 17:15:21
MySQL
Evgeny Kramor, 2018-05-16 17:15:21

How to display data from two tables by key field?

Good afternoon!
There is a table example1 which has the following fields: id, title, description, keyarticle .
And the second table is example2 which has the following fields: id, photo, linkon, keyarticle .
Can you please tell me how to correctly link two tables and display them in one whole?
Something tried like this:

SELECT * FROM example1, example2 WHERE example1.keyArticle = example2.keyArticle

The whole problem is that the output in php:
for($i = 0; $i < count($getArticle); $i++) {
        echo $getArticle[$i]["title"]."<br>";
        /* */
    }

records are displayed with numerous repetitions. If, for example, there are five records in the tables, then it displays 5 records 5 times.
The head doesn't work at all. Can you tell me how to make a request correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery, 2018-05-16
@it_monk

SELECT * FROM example1 e1 INNER JOIN example2 e2 ON e1.keyArticle = e2.keyArticle

D
Dark_Scorpion, 2018-05-16
@Dark_Scorpion

Repetitions will be in any case if this relationship is one-to-many. Try using GROUP BY.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question