I have this query in MSSQL ..
how to convert it into MYSQL query..
CREATE VIEW DATA_VIEW AS SELECT A.*,B.FIELD_VALUE D1_VALUE,C.FIELD_VALUE D2_VALUE,D.FIELD_VALUE
C_VALUE FROM DB_DATA A
FULL OUTER JOIN T_CASE_D1 B ON A.FIELD_ID =B.FIELD_ID AND B.CASE_ID=47758
FULL OUTER JOIN T_CASE_D2 C ON A.FIELD_ID =C.FIELD_ID AND C.CASE_ID=47758
FULL OUTER JOIN T_CASE_QC D ON A.FIELD_ID =D.FIELD_ID AND D.CASE_ID=47758
WHERE A.FORM_ID=5 AND IS_ACTIVE='Y'
thanks in advance
Your where clause is turning the FULL OUTER JOIN into a LEFT JOIN anyway, so you might as well just use that:
CREATE VIEW DATA_VIEW AS
SELECT A.*, B.FIELD_VALUE D1_VALUE, C.FIELD_VALUE D2_VALUE, D.FIELD_VALUE AS C_VALUE
FROM DB_DATA A LEFT JOIN
T_CASE_D1 B
ON A.FIELD_ID = B.FIELD_ID AND B.CASE_ID = 47758 LEFT OUTER JOIN
T_CASE_D2 C
ON A.FIELD_ID = C.FIELD_ID AND C.CASE_ID = 47758 LEFT OUTER JOIN
T_CASE_QC D
ON A.FIELD_ID =D.FIELD_ID AND D.CASE_ID = 47758
WHERE A.FORM_ID=5 AND IS_ACTIVE = 'Y';
FULL OUTER JOIN is rarely needed in a database with properly maintained foreign key relationships.
Related
So I'm having a slight problem with having to save price on a product in two different tables due to a few reasons. Is it possible to merge two columns into one? I know UNION exists but does it work with LEFT JOIN's?
Any pointers is much appreciated.
Best Regards
SELECT
si.id AS shop_item_id,
si.item_price,
s.logo_file_name,
p.cat_id AS category_id,
api.item_price AS api_price,
MAX(c.campaign_desc) AS campaignDesc,
MAX(c.campaign_type_id) AS campaignType,
MAX(c.shop_id) AS campaign_shop_id,
MAX(ct.image_name) AS campaignLogo
FROM
shop_item si
LEFT JOIN
shop s ON
s.id = si.shop_id
LEFT JOIN
product p ON
si.product_id = p.id
LEFT JOIN
campaign_category cc ON
cc.category_id = p.cat_id
LEFT JOIN
campaign c ON
c.id = cc.campaign_id AND
c.shop_id = si.shop_id AND
c.show_in_pricetable = 1 AND
NOW() BETWEEN c.date_from and c.date_to
LEFT JOIN
campaign_type ct ON
c.campaign_type_id = ct.id
LEFT JOIN
shop_api_item api ON
si.rel_feed_api = api.unique_id AND si.shop_id = api.shop_id
WHERE
si.`product_id` = 586 AND
s.`active_shop` = 1
GROUP BY
s.name,
si.id ,
si.item_price
ORDER BY
si.`item_price`,
si.`shop_id`,
c.`campaign_desc` DESC
It looks like you would benefit from the COALESCE() function.
SELECT
si.id AS shop_item_id,
COALESCE(si.item_price, api.item_price) AS coalesced_price,
...
COALESCE() takes multiple arguments, and returns the first argument that is not NULL.
I am trying to make a query, which returns tipo_id from a table, depending on the value of this I want to join with another table, for example if tipo_id is 1 I want to join with table called p_read if tipo_id i want to join tv_read
this is what I tried to do.
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id, lp.id_consumo FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
IF (l.tipo_id=1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE IF (l.tipo_id =3)
INNER JOIN aldroges8.lectura_tv_gas lp ON lp.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
But i am getting this error.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (l.tipo_id==1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE' at line 7
So I want to know if there is a way on doing this if staments on a query, or do I need to make another query with tipo_id, thanks in advance
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
/* Used case when statement to get the required result in that column */
case when l.tipo_id=1 then lp_1.id_consumo
when l.tipo_id=3 then lp_3.id_consumo end as id_consumo
FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
left join aldroges8.lectura_potencia lp_1 ON l.id=lp_1.id
left join aldroges8.lectura_tv_gas lp_3 ON lp_3.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
I would write this with the condition in the on clause and then use coalesce() in the select:
SELECT ec.id, ec.estado, fv.id, fv.num_factura, fv.importe, fv.iva,
fv.total, fv.fecha_consumo_inicio, fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
coalesce(lp_1.id_conumo, lp_3.id_consumo) as id_consumo
FROM aldroges8.factura_venta fv INNER JOIN
aldroges8.lectura l
ON fv.id = l.facturaVenta_id INNER JOIN
aldroges8.factura_cobro fc
ON fc.facturaventa_id = fv.id INNER JOIN
aldroges8.cobros c
ON c.id = fc.cobros_id INNER JOIN
aldroges8.estado_cobros ec
ON ec.id = c.estado LEFT JOIN
aldroges8.lectura_potencia lp_1
ON l.id = lp_1.id AND l.tipo_id = 1 LEFT JOIN
aldroges8.lectura_tv_gas lp_3
ON lp_3.id = l.id AND l.tipo_id = 3
WHERE fv.factura_enviada = 1 AND
fv.suministro_id = :id_contrato
ORDER BY fv.fecha_factura DESC;
The difference between doing the comparison in the ON verses in a CASE expression may seem subtle, but it can be important.
If there are multiple matches in either table, then putting the condition in the SELECT will result in duplicate rows.
I have a question in my query. why I can't filter using a.train_num?. The result is always 0. But when I filter using c.stridnumber then I am getting result.
please check my below query.
SELECT a.*,b.*,c.*
FROM pos_train_db a
INNER JOIN emp_db b
ON a.pos_name = b.emp_position
INNER JOIN tms_ml c
ON b.ID = c.stridnumber
WHERE a.train_num=10
Try this:
$this->sql =
"SELECT a.*,b.*,c.* FROM pos_train_db a
LEFT JOIN emp_db b ON a.pos_name = b.emp_position
LEFT JOIN tms_ml c ON b.ID = c.stridnumber
WHERE a.train_num=10 ";
I have written the following query, which does not work. I want to know how to make it work. It is a two-JOIN query which fails to work.
SELECT oc_download.download_id, oc_product_to_download.download_id, oc_download_description.download_id
FROM oc_download
LEFT JOIN oc_product_to_download
ON oc_download.download_id = oc_product_to_download.download_id
LEFT JOIN
oc_download.download_id = oc_download_description.download_id
WHERE oc_product_to_download.product_id = 89
With single JOIN it works, but adding the second JOIN it fails. here is the clean working one-JOIN query:
SELECT oc_download.download_id, oc_product_to_download.download_id, oc_download_description.download_id
FROM oc_download
LEFT JOIN oc_product_to_download
ON oc_download.download_id = oc_product_to_download.download_id
WHERE oc_product_to_download.product_id = 89
How should I use multiple JOIN in one single query?
You forgot the table name in the 2nd join
SELECT d.download_id, p.download_id, dd.download_id
FROM oc_download d
LEFT JOIN oc_product_to_download p ON d.download_id = p.download_id
LEFT JOIN oc_download_description dd ON d.download_id = dd.download_id
WHERE p.product_id = 89
And your where clause turns your left join into an inner join. If you don't want that then change your query to
SELECT d.download_id, p.download_id, dd.download_id
FROM oc_download d
LEFT JOIN oc_product_to_download p ON d.download_id = p.download_id
AND p.product_id = 89
LEFT JOIN oc_download_description dd ON d.download_id = dd.download_id
This is your query fixed up a bit, using table aliases and proper join syntax:
SELECT od.download_id, opd.download_id, odd.download_id
FROM oc_download od LEFT JOIN
oc_product_to_download opd
ON od.download_id = opd.download_id LEFT JOIN
oc_download_description odd
od.download_id = odd.download_id
WHERE opd.product_id = 89;
You are using left join, but this appears to be unnecessary. The on clause is undoing the first outer join, turning it into an inner join (unmatched rows would have a NULL value, which are filtered out by the where clause). In fact, I would guess that your data has well defined foreign key relationships among the columns being joined. If this is the case, you should use inner join (or just join) for the query:
SELECT od.download_id, opd.download_id, odd.download_id
FROM oc_download od JOIN
oc_product_to_download opd
ON od.download_id = opd.download_id JOIN
oc_download_description odd
od.download_id = odd.download_id
WHERE opd.product_id = 89;
The left join is misleading because it implies that some keys might not match. You also run the risk of confusing the optimizer.
Hello currently i have 4 tables in my database: which are tb_student, tb_history, tb_section and tb_adviser. so far i used this line to show me the history of a given student:
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, c.adviser_id
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
WHERE a.student_id=".$id." AND a.level='1st Year'";
my main problem is now i need to show the last name of the adviser with these other information. so i was thinking of putting. Note that tb_adviser is only CONNECTED to tb_section via adviser_id
LEFT OUTER JOIN tbl_adviser AS d ON a.student_id = c.adviser_id
I added this line before there where statement. and would insert this line in my SELECT fields.
d.lname_adviser
Currently it doesn't work. Anyone would shed some light into my problem.
Seeing the db structure would help, but it looks like you are trying to have JOIN on two columns that won't match. Is a.student_id going to match c.adviser_id?
Assuming the advisor_id is id in tbl_advisor then just add
LEFT OUTER JOIN tbl_advisor AS d
ON d.id = c.advisor_id
try this:
I think you should do
LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id
So your query would be:
Select .....
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id
WHERE a.student_id=".$id." AND a.level='1st Year'