C
C
cloneforce2020-04-28 14:07:16
PHP
cloneforce, 2020-04-28 14:07:16

How to display all fields from the database without repetition?

Good afternoon.
There is a table with the following values:
| id | name |
1 | Stephen Lang, William Sadler
2 | Guy Ritchie
3 | Stephen King, Stephen Lang
------------------------------------------------------
I can not figure out how to display all the names from the table without repetition.
For example, this table should display:
Stephen Lang
William Sadler
Guy Ritchie
Stephen King
---------------------------------- --------
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
londhor, 2020-04-28
@cloneforce

filter on the backend, after unloading from the database.
In general, the question is: Why can you have several names entered in a unique id?
You have a problem in the database architecture, hence your question.
You need to assign a unique ID to each author in the database, and to the post (whatever you call it), assign an array of author IDs, instead of the text directly.
Answering the question itself:
1. Get all records.
2. Walk through the array of records, and push into the new array only those lines in which there are no repetitions of the name. The lines themselves need to be split into arrays through a comma so that you can check the uniqueness of the authors

S
Sergey Pankov, 2020-04-28
@trapwalker

Or so if postgres:

WITH data AS (
  SELECT 1 AS id, 'Стивен Лэнг, Уильям Сэдлер' AS name UNION
  SELECT 2 AS id, 'Гай Ричи' AS name UNION
  SELECT 3 AS id, 'Стивен Кинг, Стивен Лэнг' AS name
)
SELECT DISTINCT unnest(regexp_split_to_array(name, ',\s*'))
FROM data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question