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
Related
I have this db structure
and this is my joined tables (sample data)
i want to filter when (key = 'price' and value > 4000) and (key = 'top-speed' and value > 200)
thanks for help)
Try this:
SELECT
car.id,
car.name,
key,
value
FROM
car c
LEFT JOIN car_specification_value csv ON c.id = csv.car_id
LEFT JOIN specification s ON csv.specification_id = s.id
WHERE
s.key = 'price'
AND csv.value > 4000
AND s.key = 'top-speed'
AND csv.value > 200;
A common practice is giving aliases to your tables so the join conditions can be determined in a shorter way.
One method uses aggregation:
select csv.car_id
from car_specification_value csv
where (csv.key = 'price' and (csv.value + 0) > 4000) and
(csv.key = 'top-speed' and (csv.value + 0) > 200)
group by csv.car_id
having count(distinct csv.key) = 2; -- both match
Note the + 0. This uses implicit conversion to change the value to a number, so it can be properly compared to a number. One challenge of key/value data structures is that all the values are strings, and that is tricky for other data types.
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
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;
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.
I want to use a Select query from mysql database in C:
mysql_query(conn,"SELECT SI AS SUBSCRIBER_ID ,TG2 AS TAG_ID, SUM(CTR) AS NBR FROM (SELECT H.SUBSCRIBER_ID AS SI, TG.TAG_ID AS TG1,T.TAG_ID AS TG2, COUNT(TG.TAG_ID) AS COUNTER,CASE WHEN (TG.TAG_ID = T.TAG_ID) THEN COUNT(TG.TAG_ID) ELSE 0 END AS CTR from content_hits H left join CONTENT_TAG TG ON TG.CONTENT_ID = H.CONTENT_ID LEFT JOIN TAG T ON 1= 1 GROUP BY H.SUBSCRIBER_ID, TG.TAG_ID,T.TAG_ID) AS TAB GROUP BY SI,TG2");
After that, I want to use 'NBR' to fill an array of one dimension.
I tried this:
result = mysql_store_result(conn);
while ((row = mysql_fetch_row(result)))
{
t[i]=*row['NBR'];
printf("%d",t[i]);
}
But it didn't work.
You cannot access the row columns by name like you have t[i]=*row['NBR'];. Use for example fields = mysql_fetch_fields(result); to get the column names and iterate through the fields array to find which column id 'NBR' has. This id can then be used in t[i]=row[id];. This is all in the mysql connectors doc http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-fields.html