Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2013
    Posts
    6

    Unanswered: UNION with different columns

    Hi all,

    I have a problem on how to use UNION in MySQL i for two tables with different number of columns.

    1st table (user)
    user_id, name, position, manage, username and password

    2nd table (client)
    client_id, client_name, client_contact, occupation, email, member_type, username and password


    I have 2 tables with different columns, these tables (user and client) use the same log in form for them to go into their own page.
    Eg. Client will go to client's page and User will go to user's page.

    How to do UNION for this two tables? I want to do UNION for these tables for log in purpose.

    Thanks for the help
    Last edited by hujan; 03-25-13 at 05:28.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Columns and data types MUST match when using the UNION operator.

    What kind of output are you trying to achieve and why?
    George
    Home | Blog

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I find it disturbing that your question exactly matches question 6 on the pre-test that I wrote for a four day class being offered in Saint Louis on Monday.

    If you are taking that class, you're cheating yourself by appearing to know content that you haven't mastered. Tell Scott (the instructor) that I said "hello" too!

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Burned ?
    George
    Home | Blog

  5. #5
    Join Date
    Mar 2013
    Posts
    6
    To Pat Phelan,

    I have no idea what are you talking about. I am here searching for help for my school project nothing doing with any test.

  6. #6
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by gvee View Post
    Columns and data types MUST match when using the UNION operator.

    What kind of output are you trying to achieve and why?
    I am trying to joining this User and Student tables for log in purpose. As every user for the system can log in using only one log in form. And it will redirect them to the specific user_level.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    do you mean UNION or do you mean JOIN.
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by healdem View Post
    do you mean UNION or do you mean JOIN.
    Sorry for the confusion. To be honest, I'm not sure which one can I apply in my PHP code.

  9. #9
    Join Date
    Mar 2013
    Location
    Breda, Netherlands
    Posts
    15
    Just use fake columns for de select you have less columns:
    Code:
    SELECT c1, c2, c3 FROM table1
    UNION
    SELECT c1, 'foo', 'bar' FROM table2

  10. #10
    Join Date
    Dec 2012
    Location
    Logan, Utah
    Posts
    163
    Quote Originally Posted by hujan View Post

    1st table (user)
    user_id, name, position, manage, username and password

    2nd table (client)
    client_id, client_name, client_contact, occupation, email, member_type, username and password

    How to do UNION for this two tables? I want to do UNION for these tables for log in purpose.
    Not sure how a union helps with logging in. But, if I was to guess what you are looking for,

    Code:
    Select user.user_id, user.name, client.client_name
      From users as user
    Inner join clients as client on user.name = client.username

  11. #11
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by LinksUp View Post
    Not sure how a union helps with logging in. But, if I was to guess what you are looking for,

    Code:
    Select user.user_id, user.name, client.client_name
      From users as user
    Inner join clients as client on user.name = client.username
    I tried this way already.. still cannot log in to their respective page

    I have 2 tables with different columns, these tables (user and client) use the same log in form for them to go into their own page. Eg. Client will go to client's page and User will go to user's page.

  12. #12
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    So what you are trying to do is have a common logon form
    authenticate a user and then dispatch the user to the correct subsystem either client or user.
    so in your position what I'd do is test if the user exists in one table and if not test if they are in the other table
    psuedocode
    user_id, username, password from table USER
    if found
    dispatch user to User subsystem
    else
    user_id, username, password from table CLIENTS
    if found
    dispatch user to Client subsystem
    endif
    endif
    otherwise the user wasn't authenticated
    /psueodcode

    mind you I'd probably also redesign the data and merge the two entities USER and CLINET into one table, or have clinets hang off user table. it doens't feel 'right' havign two tables with what is essentaily the same data
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

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