Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

MySQL Query help!!!

crash21crash21 Member Posts: 2
Hello,

I want to make query to a table.
I want to use a variable instead table name.
For example"

SELECT * FROM table WHERE 1=1

instead of above i want to do this:

$table= ggg;
SELECT * FROM $table WHERE 1=1

I so the above but don't work....
How can i achieve this?
Moreover please inform me how can I do multiple queries with one Select command.
For example:

SELECT * FROM table,table1,table2,table3 WHERE 1=1

I can't achieve this again :(

Thanks a lot...

Comments

  • netgertnetgert Member Posts: 331
    : Hello,
    :
    : I want to make query to a table.
    : I want to use a variable instead table name.
    : For example"
    :
    : SELECT * FROM table WHERE 1=1
    :
    : instead of above i want to do this:
    :
    : $table= ggg;
    : SELECT * FROM $table WHERE 1=1
    :
    : I so the above but don't work....
    : How can i achieve this?
    : Moreover please inform me how can I do multiple queries with one Select command.
    : For example:
    :
    : SELECT * FROM table,table1,table2,table3 WHERE 1=1
    :
    : I can't achieve this again :(
    :
    : Thanks a lot...
    :
    :
    Seems like you have trouble building the query string on your programming language level, not SQL level (PHP as far as i see).
    For the first problem
    [code]
    $table = 'ggg';
    mysql_query('SELECT * FROM ' . $table); // this will append $table value to the query string
    [/code]
    WHERE 1=1 is not neccessary if you want to get all the rows - simple SELECT FROM is faster in this case

    For the second problem, your query SELECT * FROM table1, table2, table3, ... will become VERY slow as the tables grow or the more tables you use. This is because MySQL will produce all possible combinations of all the rows in the tables. To illustrate this...

    [code]
    t1:
    t1r1 | t1r2
    8 | G
    N | H
    9 | 2

    t2:
    t2r1 | t2r2
    9 | U
    8 | 7

    t3:
    t3r1 | t3r2
    1 | 5
    F | E
    [/code]

    [code]
    query: SELECT * FROM t1, t2, t3

    result:
    t1r1 | t1r2 | t2r1 | t2r2 | t3r1 | t3r2
    8 | G | 9 | U | 1 | 5
    8 | G | 8 | 7 | 1 | 5
    8 | G | 9 | U | F | E
    8 | G | 8 | 7 | F | E
    N | H | 9 | U | 1 | 5
    N | H | 8 | 7 | 1 | 5
    N | H | 9 | U | F | E
    N | H | 8 | 7 | F | E
    9 | 2 | 9 | U | 1 | 5
    9 | 2 | 8 | 7 | 1 | 5
    9 | 2 | 9 | U | F | E
    9 | 2 | 8 | 7 | F | E
    [/code]

    If you need the rows of t1 followed by the rows of t2 and finally followed by the rows of t3, you can do this:

    [code]
    query: SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3

    result:
    t1r1 | t1r2
    8 | G
    N | H
    9 | 2
    9 | U
    8 | 7
    1 | 5
    F | E
    [/code]

    Also, MySQL documentation could be your best friend
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

  • free_directoryfree_directory Member Posts: 11
    : : Hello,
    : :
    : : I want to make query to a table.
    : : I want to use a variable instead table name.
    : : For example"
    : :
    : : SELECT * FROM table WHERE 1=1
    : :
    : : instead of above i want to do this:
    : :
    : : $table= ggg;
    : : SELECT * FROM $table WHERE 1=1
    : :
    : : I so the above but don't work....
    : : How can i achieve this?
    : : Moreover please inform me how can I do multiple queries with one Select command.
    : : For example:
    : :
    : : SELECT * FROM table,table1,table2,table3 WHERE 1=1
    : :
    : : I can't achieve this again :(
    : :
    : : Thanks a lot...
    : :
    : :
    : Seems like you have trouble building the query string on your
    : programming language level, not SQL level (PHP as far as i see).
    : For the first problem
    : [code]:
    : $table = 'ggg';
    : mysql_query('SELECT * FROM ' . $table); // this will append $table value to the query string
    : [/code]:
    : WHERE 1=1 is not neccessary if you want to get all the rows - simple
    : SELECT FROM is faster in this case
    :
    : For the second problem, your query SELECT * FROM table1, table2,
    : table3, ... will become VERY slow as the tables grow or the more
    : tables you use. This is because MySQL will produce all possible
    : combinations of all the rows in the tables. To illustrate this...
    :
    : [code]:
    : t1:
    : t1r1 | t1r2
    : 8 | G
    : N | H
    : 9 | 2
    :
    : t2:
    : t2r1 | t2r2
    : 9 | U
    : 8 | 7
    :
    : t3:
    : t3r1 | t3r2
    : 1 | 5
    : F | E
    : [/code]:
    :
    : [code]:
    : query: SELECT * FROM t1, t2, t3
    :
    : result:
    : t1r1 | t1r2 | t2r1 | t2r2 | t3r1 | t3r2
    : 8 | G | 9 | U | 1 | 5
    : 8 | G | 8 | 7 | 1 | 5
    : 8 | G | 9 | U | F | E
    : 8 | G | 8 | 7 | F | E
    : N | H | 9 | U | 1 | 5
    : N | H | 8 | 7 | 1 | 5
    : N | H | 9 | U | F | E
    : N | H | 8 | 7 | F | E
    : 9 | 2 | 9 | U | 1 | 5
    : 9 | 2 | 8 | 7 | 1 | 5
    : 9 | 2 | 9 | U | F | E
    : 9 | 2 | 8 | 7 | F | E
    : [/code]:
    :
    : If you need the rows of t1 followed by the rows of t2 and finally
    : followed by the rows of t3, you can do this:
    :
    : [code]:
    : query: SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3
    :
    : result:
    : t1r1 | t1r2
    : 8 | G
    : N | H
    : 9 | 2
    : 9 | U
    : 8 | 7
    : 1 | 5
    : F | E
    : [/code]:
    :
    : Also, MySQL documentation could be your best friend
    : [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blu
    : e]ert[/italic][/blue][hr]
    :
    :
    Tip: use " instead of ' to encapsulate the query string. and if you put variables in query... the best way it's to use that format:
    "select `x` from `y` where `z`='".$variable."'
    same for x or y, if they are varriables, use instead of x ".$x."
    hope it make sens for you:)
    ---------WEB---------
    Publish your website for free here:
    Real Free Open Directory - http://www.new-dmoz.org
Sign In or Register to comment.