J
J
John Freeman2017-05-10 12:28:20
PHP
John Freeman, 2017-05-10 12:28:20

How can I correctly select from MySQL?

Hello!
I have three tables!
news
id|title|text|test|qwer|trrr


1|test name|description|wer234|qqq
2|test nam2|description|wer234|qqq
3|test na3|description|wer234|qqq
4|test n4|description|wer234 |qqq
5|test 5|description|wer234|qqq

type
id|type

1|type1
2|type2
3|type3
4|type4
5|type5

newsandtype
news_id|type_id

1|2
1|5
1|1
2|3
2|4
5|5
5|2
5|3 the
newsandtype
table is a many-to-many relationship, news and type relationship
and so, how to me correctly to make selection of records from the table news given the table type?
For example, I want to display:
1 test name description type2,type5,type1 wer234
2 test nam2 description type3,type4 wer234
5 test 5 description type5,type2,type3 wer234

display news table data + type table data only if there are links in the newsandtype table

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-05-10
@AsviS

use JOIN like

SELECT 
  news.id,
  news.title,
  news.text,
  type.type
FROM news
LEFT JOIN newsandtype ON news.id = newsandtype.news_id
LEFT JOIN type ON type.id = newsandtype.type_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question