Obtenga el valor de la misma columna dos veces con una condición diferente en una sola consulta

Hii I want to Obtain value from same column twice with different condition in single query

Aqui esta mi codigo

SELECT
    o.products_options_name,
    ov.products_options_values_name AS products_options_values_name,
    a.options_values_quantity  
FROM
    table_products_options o,
    table_products_attributes a
WHERE
    a.options_id = o.products_options_id
    AND 
    o.products_options_values_id = a.options_values_id 
    AND
    a.products_id = :products_id 
    AND
    a.options2_values_id = options2_values_id

i want to obtain value of column products_options_values_name again with different condition like where a.options2_values_id = ov.products_options_values_id in same query. How can i do this?

preguntado el 28 de mayo de 14 a las 13:05

where is alias 'ov' ? -

3 Respuestas

try by using UNION operator,

SELECT o.products_options_name, ov.products_options_values_name AS products_options_values_name,
a.options_values_quantity  
FROM 
table_products_options o, table_products_attributes a
WHERE 
a.options_id = o.products_options_id AND ov.products_options_values_id = a.options_values_id 
AND a.products_id = :products_id1 AND 
a.options2_values_id = options2_values_id 

UNION 

SELECT o.products_options_name, ov.products_options_values_name AS products_options_values_name,
 a.options_values_quantity  
FROM 
table_products_options o, table_products_attributes a
WHERE 
a.options_id = o.products_options_id AND ov.products_options_values_id = a.options_values_id
AND a.products_id = :products_id2 AND 
a.options2_values_id = options2_values_id 

contestado el 28 de mayo de 14 a las 13:05

Union / Union all would be best

como

select o.products_options_name, ov.products_options_values_name as     
products_options_values_name, a.options_values_quantity  
from 
table_products_options o, table_products_attributes a
where 
a.options_id = o.products_options_id and ov.products_options_values_id =     
a.options_values_id and a.products_id = :products_id and  a.options2_values_id = 
options2_values_id 

UNION 

select o.products_options_name, ov.products_options_values_name as products_options_values_name, a.options_values_quantity  
from 
table_products_options o, table_products_attributes a
where Diffrent Clause

Union Works like Distinct, Union ALL will return duplicate values if your clauses both return the value.

Alternatively use OR in your where clause and (a.b = c.d OR c.d=b.a) and

contestado el 28 de mayo de 14 a las 13:05

You can specify the condition that you want in the on cláusula. on clause? Your query should use explicit join sintaxis.

Here is one way to express the query that you want:

select o.products_options_name, ov.products_options_values_name as products_options_values_name, 
       a.options_values_quantity  
from table_products_options o join
     table_products_attributes a
     on a.options_id = o.products_options_id and
        (ov.products_options_values_id = a.options_values_id or
         a.options2_values_id = options2_values_id
        )
where a.products_id = :products_id;

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.