I
I
Ilya2020-06-11 17:29:20
MySQL
Ilya, 2020-06-11 17:29:20

Overgrown SQL... I see a problem, but I can't find a solution. Can you help?

There is a request:

$query = "SELECT `id`, `header`, `text`, `content`, `img`, (SELECT surname FROM personal WHERE id = (SELECT id_employee FROM news WHERE news.id_employee = personal.id) LIMIT 1) AS surname, `status`, `period` FROM `news`";


There are tables:
1) news
5ee23c1fd3c40951877069.png

2) personal The
5ee23ca0b53b5058740284.png

query outputs data to a table on the page:
5ee23d1120985677062576.png

How to write a query to:
In the "author" column from the table on the page, display the employee's last name from the personal table from the row with id = id_employee ?

(if you remove LIMIT 1 from the query, there will be an error that the query returns more than one row)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nokimaro, 2020-06-11
@ilya_96

SELECT 
    news.`id`, 
    news.`header`, 
    news.`text`, 
    news.`content`, 
    news.`img`, 
    personal.surname, 
    news.`status`, 
    news.`period` 
FROM `news`
JOIN personal ON (personal.id = news.id_employee)

G
galaxy, 2020-06-11
@galaxy

SELECT ..., p.surname FROM `news` n JOIN personal p ON (p.id = n.id_employee)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question