zjnz.com

welcome to my space

select one or/and another table

  • Hi,

    Say an user enters an input field, if it equals to a db field in table a, it will display row's information from table a; and if it equals to a db field in table b, it will display row's information from table b. So it could display info from either table a or table b, or both. How do I do that in one query select?

    Thanks,
    John


  • Thanks, Elena.

    So "select if" is implemented in Mysql 4.0? Is it in the manual somewhere?

    I asked the same question at
    http://www.sitepointforums.com/showthread.php?s=&postid=619685#post619685
    and received different answers, so I might try both.

    select if(a.field1=20,a.field1,NULL)
    20? is it the form input field that user enters?


  • 1)yes, 20 is that values that you receive from input field.
    2) I found IF operator in list of functions for MYSQL_FRONT, but I am sure you can find informaiton about this operator in any MySQL manual. That's is description of IF operator thatI found:

    IF(expr1,expr2,expr3)If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2, else it returns expr3

    3) about UNION operator, surely I know that it would be easier to solve your problem using this operator, problem is that it was implemented starting from MySQL 4.0, my MySQL version is 3.23 and doesn't support UNION. But if your MySQL version is 4.0 or higher, do you query with UNION.


  • Here is query that I could quicly think up regarding your question
    This query selects data that contains or in one table, or in another table, or in both tables:

    select if(a.field1=20,a.field1,NULL) as 'a', if(b.field1=20,b.field1,NULL) as 'b' from a, b
    where a.field1=20 or b.field1=20 order by 1 desc ,2 desc limit 1 ;

    Here I used two tables a and b:
    a___________________b_______
    field1______________field1____

    "20" is that criteria by which you will take data from tables.
    Sorry if it sounds confusing, ask me, "ill explain simplier







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about select one or/and another table , Please add it free.
    edit

    Mexico