views:

123

answers:

5

I have two tables in a MySQL Database.

Users table and Users Meta Table

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

This is all information relating to the users id number as well. So for example user_id = 1.

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

Structure Example

Users Table

  • user_id
  • user_login
  • user_pass

Users Meta Table

  • user_meta_id
  • user_id
  • user_meta_key
  • user_meta_value

Im wanting 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
+1  A: 

Sure, that's easy, just specify the fields you want from each table in the query with a join and don't include the columms that are repeated.

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)
JohnFx
May want to left join if you want to get all the records from users even if they don't have records in meta.
invertedSpear
Please don't promote non-ANSI queries. Should be: `select u.Field1, u.Field2, u.Field3, u.user_id, um.Field4, um.field5 from Users u inner join UsersMeta um on u.user_id=um.user_id`
RedFilter
My reading of the question made me imagine a union query ... maybe more information needed.
Smandoli
@Smandoli: I'm starting to think that too. I'll wait until the OP offers some clarification and adjust my answer accordingly, if necessary.
JohnFx
@JohnFX: This still repeats the data for user_id, user_login, user_pass
Aran
@Aran: Can you show a tabular view in the question to explain exactly the output you are looking for? I'm confused.
JohnFx
@JohnFX: Something like that or similar... Basically want to be able to get all the information in one query but without duplicating all the data.
Aran
Aha! That's not exactly what a relational database system is designed to do. You seem to be commingling the presentation of the data with the querying of it. Normally you just query it with the repeating columns and let the reporting tool format it for display the way you have indicated. What you are asking for can be done, but the query is a little hairy and probably will perform badly. My advice is to not try to make the query do the formatting work in the first place.
JohnFx
Ah ok... I was hoping to find a way to reduce the amount of data being queried from the database.
Aran
If you are really concerned about the amount of data coming down the wire, I'd suggest splitting it into multiple queries: one for the user info, and one for the meta info.
JohnFx
That is what I am going to have to do...
Aran
A: 

You didn't mention what 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)

Your query would look like this in MySQL:

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);

Saheed
A: 

yes, it is possible using DISTINCT keyword in query

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

Distinct description

Anton
A: 

That's the work of front-end tools/language, e.g. Crystal Report, PHP, C#, etc. Don't do it in query

Michael Buen
A: 

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)

user5678