Data from two tables without repeating data from the first?

I have two tables in a MySQL database.

User Table and Meta Table User Table

I am looking for a way to get all the information from both tables with a single query. But without repeating the information from the Users table.

This is all information related to the user number. For example, user_id = 1.

Is there a way to query the database and collect all the information I have from both tables without repeating the information from the first?

Structure example

User table

  • user_id
  • user_login
  • user_pass

User Meta Table

  • user_meta_id
  • user_id
  • user_meta_key
  • user_meta_value

I want to get out of this

user_id, user_login, user_pass, user_meta_id, user_id, user_meta_key, user_meta_value
                                user_meta_id, user_id, user_meta_key, user_meta_value
                                user_meta_id, user_id, user_meta_key, user_meta_value
+3
source share
5 answers

, , , , columms.

SELECT Users.field1, Users.field2, Users.field3, Users.user_ID, 
       UsersMeta.field4, UsersMeta.field5
FROM USERS
LEFT JOIN UsersMeta ON (Usuers.user_ID=UsersMeta.User_ID)
+1
SELECT DISTINCT table1.id, table1.field2, table1.field3, tab_id.id_table1 
FROM table1 
LEFT JOIN tab_id ON (table1.id=tab_id.id_table1)
+1

You did not specify which database server you are using.

Assuming your tables are like:

USERS (user_id, first_name, last_name, gender)
USER_META (user_id, comment_count, likes)

In MySQL, your query will look like this:

SELECT u.user_id, first_name, last_name, gender, comment_count, likes 

FROM USERS u LEFT JOIN USER_META m ON (u.user_id = m.user_id);

0
source

yes, it is possible to use a keyword DISTINCTin the request

    SELECT DISTINCT Users.field1, DISTINCT users.field2, UsersMeta.field3
          FROM USERS, UsersMeta
    WHERE Users.user_ID=UsersMeta.User_ID

Great description

0
source

This is the work of front-end tools / language, for example. Crystal Report, PHP, C #, etc. Do not do this in the request

0
source

Source: https://habr.com/ru/post/1742184/


All Articles