Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Senior Member Seldimi's Avatar
    Join Date
    Dec 2002
    Location
    Kozani, Greece
    Posts
    488
    Member #
    409
    Hello, i know how to do this
    Code:
    SELECT * FROM table blabla
    When I need to select from some tables I write
    Code:
    SELECT * FROM table1 and table2 and table3
    or is that wrong ???
    - Webmaster's Planet . Greek Vortal For Webmasters ...
    - MyPortFolio - View My Creations

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Code:
    SELECT table1.*, table2.*, table3.*
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    filbert's got it right... that's the proper way to select things. table.field
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  5. #4
    Senior Member Seldimi's Avatar
    Join Date
    Dec 2002
    Location
    Kozani, Greece
    Posts
    488
    Member #
    409
    Gee, thanks guys
    - Webmaster's Planet . Greek Vortal For Webmasters ...
    - MyPortFolio - View My Creations

  6. #5
    Senior Member Seldimi's Avatar
    Join Date
    Dec 2002
    Location
    Kozani, Greece
    Posts
    488
    Member #
    409
    So it will be
    Code:
    SELECT * FROM table.*, table2.*, table3.* BLABLA
    Right ???
    - Webmaster's Planet . Greek Vortal For Webmasters ...
    - MyPortFolio - View My Creations

  7. #6
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    No; use the one I did.

    You can also use:
    Code:
    SELECT t1.col1, t2.col2 FROM table1 t1, table2 t2
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  8. #7
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Interesting topic. I think it's worth bringing up JOINs in this thread.

    When selecting from multiple tables, it's important to specify how you will JOIN the data that's coming from them. If one table is dependant upon the other, then the way you would join the data would be different than from two tables of the same heirarchical level.

    Say I want to select all of my users from the user table, and at the same time, I want to find out what groups they belong to (group table). Well, I probably have a table between the two that relates users to groups with a user_id and a group_id specified. This is an abstraction of a many-to-many relationship. To select the data, I would say:
    Code:
    SELECT u.id, u.first_name, u.last_name, g.name user_group
    FROM user u
        INNER JOIN user_group ug ON u.id = ug.user_id
        INNER JOIN group g ON ug.group_id = g.id
    This returns only users that are assigned to groups, and will return no data if there are 10 users, 10 groups, but no relationships defined.

    I could take it another step by doing this:
    Code:
    SELECT u.id, u.first_name, u.last_name, g.name user_group
    FROM user u
        LEFT JOIN user_group ug ON u.id = ug.user_id
        LEFT JOIN group g ON ug.group_id = g.id
    What this does is says "ok, give me all users, and all their groups, but if they don't have any groups, still give me the users, showing NULL values for the group_names".

    I could also go all the way and do this:
    Code:
    SELECT u.id, u.first_name, u.last_name, g.name user_group
    FROM user u
        FULL OUTER JOIN user_group ug ON u.id = ug.user_id
        FULL OUTER JOIN group g ON ug.group_id = g.id
    This says "ok, give me all of my users AND groups, and join them where they are related.

    Or, I could totally normalize the data and say
    Code:
    SELECT id, first_name + ' ' + last_name name, 'U' type FROM user 
    UNION ALL
    SELECT id, name, 'G' type FROM group
    This will pull all of my users and all of my groups together into one dataset as same-level items, ignoring the relationships between them.

  9. #8
    Senior Member Seldimi's Avatar
    Join Date
    Dec 2002
    Location
    Kozani, Greece
    Posts
    488
    Member #
    409
    Hmm, let me be more specific.
    Here is the query I run for 1 table
    Code:
    $query = "SELECT * FROM php_scripts ORDER BY added DESC LIMIT 0, 5";
    (That selects from one database)
    What to change in order to do the above thing but selecting in 4 tables, graphics,scripts,hosting,tutorials.
    (Sorry If I don't understand )
    - Webmaster's Planet . Greek Vortal For Webmasters ...
    - MyPortFolio - View My Creations

  10. #9
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    How are they related to eachother?

  11. #10
    Senior Member Seldimi's Avatar
    Join Date
    Dec 2002
    Location
    Kozani, Greece
    Posts
    488
    Member #
    409
    They all have the row "added" I need
    - Webmaster's Planet . Greek Vortal For Webmasters ...
    - MyPortFolio - View My Creations


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:11 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com