Y
Y
Yrets1692021-03-22 15:59:27
JavaScript
Yrets169, 2021-03-22 15:59:27

How to properly pull from two mysql tables?

users
605891a0e5e4b476190339.jpeg

table guests table
605891b5506ab695952193.png

How to form a query to get a result like this:

fio
Ivanov ( last_name from the users table)
Petrov (last_name from the users table)
.... etc. (from the last_name users table)
Vasya Pupkin (guestname from the guests table)
... etc. (guestname from guests table)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VitalyChaikin, 2021-03-22
@Yrets169

SELECT DISTINCT last_name FROM users 
UNION ALL
SELECT DISTINCT guestname FROM guests

A
Anton Anton, 2021-03-22
@Fragster

https://dev.mysql.com/doc/refman/8.0/en/union.html

T
ThunderCat, 2021-03-22
@ThunderCat

table guests - whoinvited should store the id of the inviter, then you can join these tables by id and huinvited, but in any case - in one line you will not get ALL invitees in separate fields, or these will be lines duplicated in the user table and differing only in invited , or you can make a group buy huinvited and concat on the guestname, and there will be 1 field with a string value of all names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question