V
V
Vladimir Golub2021-07-27 17:53:48
SQL
Vladimir Golub, 2021-07-27 17:53:48

How to take the value from the column of the attached table with LEFT JOIN if the parent is already in the selection?

I want to replace UNION with LEFT JOIN.
Both tables have id and name fields.
You need to merge both tables into one.

SELECT id, name
FROM articles 
INNER JOIN articles_authors ON articles.id = articles_authors.id
WHERE IF(articles.id != '', articles.id, articles_authors.id)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-07-27
@Stalker_RED

SELECT articles.id, -- можно указывать не только имя столбца, но и таблицу
foo.id, authors.name, bar.id, -- можно давать таблицам псевдонимы (alias)
bar.id as QWERTY, -- еще можно так
bar.id ZZZ -- и даже так

FROM articles foo -- foo псевдоним для таблицы articles
INNER JOIN articles_authors bar -- bar - псевдоним для articles_authors
  ON articles.id = articles_authors.id
WHERE IF(articles.id != '', articles.id, articles_authors.id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question