Compare varchar and integer in MySQL JOIN clause - mysql

I have a query as following:
select q.question, e.filename
from question q
join actions a on a.FK_QId = q.PK_Id
join action_params ap on ap.FK_AId = a.PK_Id
join envfile e on e.FK_Qid = ap.value
where e.EnvType = 0
The problem is the JOIN ON clause e.FK_Qid = ap.value where value is varchar and FK_QId is integer. I know I have to do something with CAST and CONVERT but what I tried failed:
select q.`question`, e.filename
from question q
join actions a on a.FK_QId = q.PK_Id
join action_params ap on ap.FK_AId = a.PK_Id
join envfile e on CAST(e.FK_Qid as nvarchar(3)) = ap.value
where e.EnvType = 0
Basically the value field will have strings like "1", "2" etc. So its alright if I could do either string to string comparison or int to int conversion. Bottom line is "1" = 1 and "2" = 2 and so on.
Edit: CAST(e.FK_Qid as char3) = ap.value worked as well..

If ap.value has only values like "1", "2"..., then you can use your first query, server will do everething itself. All values will be converted to numbers, and your ON condition will work.

Related

How can i set a string values based on integer variable while executing sql query?

SQL Query
sessionFactory.getCurrentSession().createSQLQuery("select claim.encounterId, claim.claimUniqID, patientmaster.FirstName, tbl_insurance.insurance_name, claim.status from rcmdb.claim join rcmdb.encounter on claim.encounterID=encounter.encounterID join rcmdb.insurance_details on encounter.insuranceDetailsID=insurance_details.insuranceDetailsID
join rcmdb.tbl_insurance on insurance_details.insurance=tbl_insurance.insurance_id
join rcmdb.patientmaster onpatientmaster.patientMasterID=encounter.patientMasterID
where createdByDate between'"+from+"' and '"+to+"'").list();
i want to return string values based on claim.status values like if the status is 1 accepted, in output I want the string values how can I write the query?
You can use CASE statement. https://www.w3schools.com/sql/func_mysql_case.asp
SELECT CASE
WHEN status =1 THEN STRING
ELSE NULL
END
Put a table in the database that maps the int to the string and join it:
ClaimStatus
--------------
ID, StatusDescription
1, Accepted
2, Rejected
SELECT c.PolicyNumber, c.ClaimValue, cs.StatusDescription
FROM
claims c
INNER JOIN claimstatus cs ON c.ClaimStatusId = cs.ID

MySQL query getting a string value from query result

I have a query as follows:
SELECT *
FROM tb_circulares LEFT JOIN tb_colegios ON tb_circulares.colegio_circular = tb_colegios.id_colegio
LEFT JOIN tb_circulares_clase ON tb_circulares.codigo_circular = tb_circulares_clase.circular
LEFT JOIN tb_clases ON tb_circulares_clase.clase = tb_clases.id_clase
WHERE colegio_circular = 17
The query output shows three rows.
One of the row columns is the value for the field tb_circular_clase.nombre_clase
I would like to get a string containing the three resulting values for tb_circular_clase.nombre_clase.
For example:
row1-> nombre_clase = "1º primaria"
row2-> nombre_clase = "3ª secundaria"
row3-> nombre_clase = "4º primaria"
Is it possible to get a resulting query field with the final value?
`resultado = "1º primaria - 3ª secundaria - 4º primaria"
Thanks
Sounds like you're after group_concat(), which is an aggregation function concatenating all values of a group.
SELECT group_concat(nombre SEPARATOR ' - ') resultado
FROM tb_circulares
LEFT JOIN tb_colegios
ON tb_circulares.colegio_circular = tb_colegios.id_colegio
LEFT JOIN tb_circulares_clase
ON tb_circulares.codigo_circular = tb_circulares_clase.circular
LEFT JOIN tb_clases
ON tb_circulares_clase.clase = tb_clases.id_clase
WHERE colegio_circular = 17;

Mysql - How to use field data as POINT() function

I have a sql like this:
SELECT
userAddress.user_address_complete,
userAddress.user_address_point,
deliveryZone.delivery_zone_id,
St_contains(deliveryZone.delivery_zone_polygon,
Geomfromtext('POINT(userAddress.user_address_point)')) AS cnt
FROM user_addresses userAddress
LEFT JOIN delivery_zones deliveryZone
ON (deliveryZone.restaurants_id = 154
AND St_contains(deliveryZone.delivery_zone_polygon,
Geomfromtext('POINT(userAddress.user_address_point)'))
> 0)
WHERE userAddress.user_address_user_id = 1
problem is that POINT(userAddress.user_address_point) should use userAddress.user_address_point field data, but sql can't understand that it is a field name and behave with it like a string so we have not result.
any suggestion?
Try to exclude column name from string. Split it into
'POINT('userAddress.user_address_point'))
SELECT
userAddress.user_address_complete,
userAddress.user_address_point,
deliveryZone.delivery_zone_id,
St_contains(deliveryZone.delivery_zone_polygon,
Geomfromtext('POINT('userAddress.user_address_point')')) AS cnt
FROM user_addresses userAddress
LEFT JOIN delivery_zones deliveryZone
ON (deliveryZone.restaurants_id = 154
AND St_contains(deliveryZone.delivery_zone_polygon,
Geomfromtext('POINT('userAddress.user_address_point')'))
> 0)
WHERE userAddress.user_address_user_id = 1

how to perform count using nested select in a select statement

So here is the issue. I'm trying to write a new fillrate report because the one built in is not good enough... I'm trying to run a single select statement to return both, a count of how many times an item was ordered for a specific month, and then also a count of how many times it was invoiced/shipped in full.
This code is obviously wrong, I also currently have it restricted to only look at AUG of 2015, but that is just to simplify results during testing.
I can't figure out how to do the 2nd count... This is what I was trying (brain stuck on old for each loop logic):
select inv_mast.item_id,
inv_mast.item_desc,
"YEAR" = year(oe_line.required_date),
"MONTH" = month(oe_line.required_date),
"ORDERS" = count(1),
"HITS" = (
select count(1)
from invoice_line
where invoice_line.order_no = oe_line.order_no
and invoice_line.oe_line_number = oe_line.line_no
and invoice_line.qty_shipped = oe_line.qty_ordered
)
from oe_line,
inv_mast,
inv_loc
where inv_mast.inv_mast_uid = oe_line.inv_mast_uid
and inv_mast.delete_flag = 'N'
and inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
and inv_loc.location_id = '101'
and year(oe_line.required_date) = '2015'
and month(oe_line.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(oe_line.required_date),
month(oe_line.required_date)
order by inv_mast.item_id
To me it would seem like you could rewrite the query to use a left join on the invoice_line table instead. Without any proper test data I can't guarantee it is correct, but I think it should be.
Besides the left join I also changed to explicit joins and moved the aliases as I don't think MySQL supports the alias = column syntax.
select inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date) as "YEAR",
month(o.required_date) as "MONTH",
count(1) as "ORDERS",
count(invoice_line.order_no) as "HITS"
from oe_line o
join inv_mast on inv_mast.inv_mast_uid = o.inv_mast_uid
join inv_loc on inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
left join invoice_line on invoice_line.order_no = o.order_no
and invoice_line.oe_line_number = o.line_no
and invoice_line.qty_shipped = o.qty_ordered
where inv_mast.delete_flag = 'N'
and inv_loc.location_id = '101'
and year(o.required_date) = '2015'
and month(o.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date),
month(o.required_date)
order by inv_mast.item_id;

Excluding a value from the return result of MySQL

I'm facing a problem and I'm not finding the answer. I'm querying a MySql table during my java process and I would like to exclude some rows from the return of my query.
Here is the query:
SELECT
o.offer_id,
o.external_cat,
o.cat,
o.shop,
o.item_id,
oa.value
FROM
offer AS o,
offerattributes AS oa
WHERE
o.offer_id = oa.offer_id
AND (cat = 1200000 OR cat = 12050200
OR cat = 13020304
OR cat = 3041400
OR cat = 3041402)
AND (oa.attribute_id = 'status_live_unattached_pregen'
OR oa.attribute_id = 'status_live_attached_pregen'
OR oa.attribute_id = 'status_dead_offer_getter'
OR oa.attribute_id = 'most_recent_status')
AND (oa.value = 'OK'
OR oa.value='status_live_unattached_pregen'
OR oa.value='status_live_attached_pregen'
OR oa.value='status_dead_offer_getter')
The trick here is that I need the value to be 'OK' in order to continue my process but I don't need mysql to return it in its response, I only need the other values to be returned, for the moment its returning two rows by query, one with the 'OK' value and another with one of the other values.
I would like the return value to be like this:
'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'status_live_attached_pregen'
for my query, but it returns:
'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'OK'
'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'status_live_attached_pregen'
Some help would really be appreciated.
Thank you !
You can solve this with an INNER JOIN on the self I think:
SELECT o.offer_id
,o.external_cat
,o.cat
,o.shop
,o.item_id
,oa.value
FROM offer AS o
INNER JOIN offerattributes AS oa
ON o.offer_id = oa.offer_id
INNER JOIN offerattributes AS oaOK
ON oaOK.offer_id = oa.offer_id
AND oaOK.value = 'OK'
WHERE o.cat IN (1200000,12050200,13020304,3041400,3041402)
AND oa.attribute_id IN ('status_live_unattached_pregen','status_live_attached_pregen','status_dead_offer_getter','most_recent_status')
AND oa.value IN ('status_live_unattached_pregen','status_live_attached_pregen','status_dead_offer_getter');
By doing a self-JOIN with the restriction of value OK, it will limit the result set to offer_ids that have an OK response, but the WHERE clause will still retrieve the values you need. Based on your description, I think this is what you were looking for.
I also converted your implicit cross JOIN to an explicit INNER JOIN, as well as changed your ORs to IN, should be more performant this way.