57 users online (0 members and 57 guests)  


  Results 1 to 2 of 2

Related

  1. help with mysql query    Forum: Databases
    Replies: 2
  2. MySQL query    Forum: PHP Forum
    Replies: 1
  3. query    Forum: HTML Forum
    Replies: 1
  4. IE div height query    Forum: CSS Forum
    Replies: 5
  5. My sql query    Forum: PHP Forum
    Replies: 0
  1. #1
    mikelindsey's Avatar
    New User

    Status
    Offline
    Join Date
    Jan 2010
    Posts
    3

    Smile Help with SQL query

    Hello All,

    Wondering if this is possible...

    I have two tables

    table 1
    T1Col1
    1
    2
    3


    table 2
    col1 col2
    -1 4
    2 5
    -1 6

    I need to show this
    T1Col1 col2
    1 4
    2 4
    3 4
    1 6
    2 6
    3 6

    so basically I need a record for each row in table one
    for each value in table2 where col1=-1

    I can't use cursors - looking for straight sql statement.

    Right now I'm planning on building the sql in php and
    using "union" to put them all together but was wondering
    if there is a straight sql way to accomplish this.

    Thanks!

  2. #2
    LarryInNyack's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2010
    Posts
    1

    Re: Help with SQL query

    A Cartesion join will work [see note at end]:

    Select T1.T1Col1, T2.col2
    from T1, T2
    Where
    --no join predicate ->> produces a cartesian product
    T2.Col1 = -1
    Order by T2.col2, T1.T1Col1;

    Note: this will work ONLY if [per your example] 4 and 6 appear only once each in T2.col2 [a unique column].