Results 1 to 7 of 7

Thread: [RESOLVED] Unite Two Tables in Database PHP-SQL

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Resolved [RESOLVED] Unite Two Tables in Database PHP-SQL

    Hello

    I have 2 Databases Each one has a table called files.

    Each Table contains 5 fileds (id,filename,filetag,filessize,filedate) and the id filed is autoincremental

    I want to unite those 2 tables into one big table i mean i put one table under the other table which makes another big table which has also the id field autoincremental and also has all the other fields

    Here is a picture of what i have in mind..




    I hope that someone will help me

    Thanks in advance

    Cheers
    Adel

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Unite Two Tables in Database PHP-SQL

    I can think of two things that you might mean:

    1. Combine the tables permanently.
      Use an INSERT query that is based on a SELECT. For example:
      Code:
      INSERT INTO table1 (
        filename,
        filetag,
        filesize,
        filedate
      )
      SELECT
        filename,
        filetag,
        filesize,
        filedate
      FROM table2;
      Since you are not selecting the table2.ID field, IDs will be generated for each row imported into table1.

    2. Combine data from the two tables into a single resultset.
      Use a UNION ALL query or create a VIEW.
      I don't think this is what you mean though.


    Hope that helps.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Unite Two Tables in Database PHP-SQL

    Thanks for your response

    But I have the 2 tables in 2 different databases so how do i do that?

    sorry for my newbish questions but i am still learning


    oh and another thing please how do i take a backup of a table of the database on the server not to download it just take it on the server..

    Thanks again

    Adel
    Last edited by lordadel; Apr 15th, 2008 at 08:38 AM.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Unite Two Tables in Database PHP-SQL

    You didn't say which of the two options you meant.

    Assuming you are using MySQL: you can reference a table in another database simply by prepending the schema name.

    Code:
    INSERT INTO db1.tablename (...) SELECT (...) FROM db2.tablename
    To back up a table (again, assuming MySQL) use mysqldump. If you don't have shell access, then you can make a rudimentary sort of backup using a tool like phpMySQL. If this isn't your server, ask your host how to do it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Unite Two Tables in Database PHP-SQL

    Thanks alot for your reply and help

    Edit: by the way it gave me mysql error "Operand should contain 1 column"

    I removed the brackets ( ) after SELECT and it worked fine

    Just wanted to let you know

    Thanks again
    Last edited by lordadel; Apr 16th, 2008 at 12:11 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: [RESOLVED] Unite Two Tables in Database PHP-SQL

    Hey Sorry

    I face another problem now i want to do that in php and the problem is that both databases have different usernames and passwords i tried to connect to both of them and it didn't work

    would you please tell me how can i do that?

    Thanks in advance
    Cheers
    Adel

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Unite Two Tables in Database PHP-SQL

    You will need one MySQL user account that has permission to read from both databases.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width