SQL doubt..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nexusbr
    New Member
    • Dec 2007
    • 15

    SQL doubt..

    Code:
    Table1: Produtos
    Codprod, Descricao
    1          , Product One
    2          , Product Two
    Code:
    Table2: Ped_det
    codprod, codprod2, etc etc
    1          ,  2
    2          ,  1


    Code:
    select b.descricao as "Código_1", b.descricao as "Código_2", quant as "Qtd", tam as "Tamanho", preco as "Preço", borda as "Borda", obs as "Observações" 
    from ped_det a , produtos b
    where a.codprod = b.codprod 
    and a.codprod2 = b.codprod and codped =   2

    the output i want is like this:
    Code:
    Product One, Product Two
    Product Two, Product One
    what i`m getting is something likt this:
    Code:
    Product One, Product One
    Product Two, Product Two
    thanks for the help
    Last edited by Atli; May 24 '09, 04:38 AM. Reason: Added a couple of line-breaks to the query.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Are you sure those are the correct values?

    I ask because your query asks for rows where the `ped_det` table has the same value for both `codprod` and `codprod2`, but in the list of rows you gave us, no row has the same values twice.

    It should be giving you an empty set.

    Another thing; in your SELECT query, the first two columns you list (Código_1 and Código_2) are in fact the same column of the same table (b.descricao), and will therefore always be the same value.

    If you want it to return values from two different rows in the same table, you will have to either subquery them separately or join the table twice.
    For example (using subqueries):
    [code=mysql]
    SELECT
    (SELECT `descricao` FROM `produtos`
    WHERE `codprod` = o.`codprod`) AS 'First',
    (SELECT `descricao` FROM `Product`
    WHERE `codprod` = o.`codprod2`) AS 'Second'
    FROM `ped_det` AS o;[/code]

    Comment

    • nexusbr
      New Member
      • Dec 2007
      • 15

      #3
      THanks!!!!! it worked!!

      :)

      Comment

      Working...