My SQL is something like this:
SELECT
NAME,
PLACE,
ETC,
format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE
FROM
THE_TABLE
And my Eloquent query is:
theTable::select(
'NAME',
'PLACE',
'ETC',
'format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE'
)
->get();
But it doesn't work because Eloquent interprets it something like select name, place, etc, format sum from the_table.
I have treid to use DB::raw but then it says that it didn't expect object.
theTable::select(
'NAME',
'PLACE',
'ETC',
DB::raw('format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE')
)
->get();
So, how can I use raw SELECT statement in Eloquent?
Use this way
DB::table('THE_TABLE')
->select(DB::raw('
NAME,
PLACE,
ETC,
format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE
'))
->get();
Related
Is it possible to simplify this sql query? Lots of calculations are reused and it would be nice to name each expression and use the name instead of the full expression.
SELECT SUM(T2.price * T1.amount) As price,
(SUM(T2.price * T1.amount) - (SUM(T2.price * T1.amount) * (T3.discount / 100))) As base_price,
((SUM(T2.price * T1.amount) - (SUM(T2.price * T1.amount) * (T3.discount / 100))) * (T3.vat / 100)) As vat_amount,
(((SUM(T2.price * T1.amount) - (SUM(T2.price * T1.amount) * (T3.discount / 100))) * (T3.vat / 100)) + (SUM(T2.price * T1.amount) - (SUM(T2.price * T1.amount) * (T3.discount / 100)))) As total_price
I'm looking for a solution similar to this:
SELECT SUM(T2.price * T1.amount) As price,
(price - (price * (T3.discount / 100))) As base_price,
(base_price) * (T3.vat / 100)) As vat_amount,
(vat_amount) + (base_price) As total_price
I tried doing it with CTE's and subqueries as suggested but it ended up looking pretty complicated and undreadable so I decided to just do the calculations outside of sql (php in this case)
Maybe something like that:
select
(A.price - (A.price * B.p_discount)) As base_price,
((A.price - (A.price * B.p_discount)) * B.p_vat) As vat_amount,
(((A.price - (A.price * B.p_discount)) * B.p_vat) + (A.price - (A.price * B.p_discount))) As total_price
from
( select SUM(T2.price * T1.amount) As price, ...
from T1 join T2 on ....
) A
join
(select (T3.discount / 100) as p_discount , (T3.vat / 100) as p_vat ,*
from T3
) B on ...
I am trying to run subqueries from another table in a query
My Query is as follows:
SELECT *, (6371000 * acos(cos(radians(select point_oi.lng
from point_oi
where point_oi.name like '%Main Square%')
)
* cos(radians(restaurants.lat)) * cos(radians(restaurants.lng)
- radians(select point_oi.lng
from point_oi
where point_oi.name like '%Main Square%'
))
+ sin(radians(select point_oi.lng
from point_oi
where point_oi.name like '%Main Square%'))
* sin(radians(restaurants.lat)))) AS distance
FROM restaurants
HAVING distance < 500;
When I run the Query I get an error saying that there is an error near select.
I would like to use the nested select queries to get the lat and lng from another table rather than hardcoding the values.
How can I fix this.
Thank you for your help
You should not use subquery for retrieve point_poi lat, lnt if the suquery return more than a rows you have error ..
try use a proper join (in this case do the fatc you have not relation between point_poi and restaurants you could use cross join )
SELECT restaurants.*,
(6371000 * acos(cos(radians(point_oi.lng ))
* cos(radians(restaurants.lat)) * cos(radians(restaurants.lng)
- radians(point_oi.lng ))
+ sin(radians(point_oi.lng ))
* sin(radians(restaurants.lat)))) AS distance
FROM restaurants
CROSS JOIN point_oi
WHERE point_oi.name like '%Main Square%'
AND (6371000 * acos(cos(radians(point_oi.lng ))
* cos(radians(restaurants.lat)) * cos(radians(restaurants.lng)
- radians(point_oi.lng ))
+ sin(radians(point_oi.lng ))
* sin(radians(restaurants.lat)))) < 500;
Please try this query and let me know is your issue resolve.
SELECT t.*
,(6371000 * acos(cos(radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) - radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) + sin(radians(SELECT point_oi.lng FROM point_oi WHERE point_oi.name LIKE '%Main Square%')) * sin(radians(restaurants.lat)))) AS distance
FROM restaurants As t
WHERE distance < 500;
I want to perform the following SQL statement in PHP using Laravel's Eloquent model:
SELECT *, (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat )))) AS distance
FROM example_retailers
HAVING distance < 1000
ORDER BY distance
OFFSET 4
LIMIT 3;
I am struggling to translate this into a Laravel's Eloquent Model (mainly the calculated column).
This is what I have so far:
ExampleRetailer::
// TODO: add calculated row
where('distance', '<', 100)
->orderBy('distance')
->skip(4)
->limit(3)
->get();
You can use raw queries (source )
DB::table('example_retailers')
->select(DB::raw('*, (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat )))) AS distance'))
->having('distance', '<', 100)
->orderBy('distance')
->skip(4)
->limit(3)
->get();
If it's only to make a new row using SQL:
SELECT *, CASE WHEN (3959 * acos(cos(radians(37))
* cos(radians(lat))
* cos(radians(lng) - radians(-122))
+ sin(radians(37))
* sin(radians(lat )))) < 100 THEN END AS NewRow
FROM example_retailers
I am trying to count of coupons sold by each store from the list of stores within 20 miles range. I know the following syntax will work if there is only 1 store.
SELECT sum(couponscount) as count where restaurant IN (SELECT storename where bhal bhal bhal and output is one value)
What is I the IN (SELECTstorenamewhere bhal bhal bhal and output is multiple values) will return multiple values?
Like in my case the complete SQL is like and its not working
SELECT sum(couponscount) as count FROM `coupons` having `restaurant` IN (SELECT `storename`, ((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) AS `distance` FROM `stores` WHERE `status`=’active’ HAVING `distance` <=20)
Is there anyway to make it working?
SELECT sum(couponscount) AS COUNT,restaurant
FROM `coupons`
WHERE `restaurant` IN
(SELECT `storename`
FROM `stores`
WHERE `status`='active'
AND
((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI()) *60 * 1.1515) <=20)
GROUP BY restaurant
Also use proper quotes for active.
Presumably, you want to get the count of coupons from stores within a distance of 20. Moving the having condition to a where clause should do what you want:
SELECT sum(couponscount) as count
FROM `coupons`
WHERE `restaurant` IN (SELECT `storename`
FROM `stores`
WHERE `status` = 'active' AND
((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) <= 20
);
You had a major syntax problem because your subquery returned two columns. When you use a subquery with in, you can only return one column, in this case, storename. I moved the code for the distance calculation to the where clause. No having clause is needed either in the subquery or the outer query.
I recently posted this question: getting distance between two points using google gps api with php
and I received a reply which was awesome however I had some questions about the person's response and due to my low/new status to this site I wasn't able to comment back. In response to my question the other user posted this query
**
SELECT a.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($lat - lat) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(lat * pi()/180) *
POWER(SIN(($long - longi) * pi()/180 / 2), 2) )) as distance
FROM table
GROUP BY id HAVING distance <= 500
ORDER by distance ASC**
I had a few questions about this query and was hoping somebody could help.
1.What is the a.*? I'm not super advanced in sql but pretty efficient and have never seen something like this. I don't know if it is supposed to represent an arbritrary field or an actual field in my table
2.Since I'm doing this in php the query will go in quotes which will then make this query syntax slightly different. I was wondering if someone knew what the query would look like in quotes.
3.There is also "GROUP BY id" in this query. I do have an id field in my table that I'm querying from. Is this "id" associated with my id field in my table?
any help would be awesome.
Your query should be like this :-
a is table alias here.
SELECT a.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($lat - lat) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(lat * pi()/180) *
POWER(SIN(($long - longi) * pi()/180 / 2), 2) )) as distance
FROM table AS a
GROUP BY id HAVING distance <= 500
ORDER by distance ASC
Or you can use simple
SELECT *, 3956 * 2 * ASIN(SQRT( POWER(SIN(($lat - lat) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(lat * pi()/180) *
POWER(SIN(($long - longi) * pi()/180 / 2), 2) )) as distance
FROM table
GROUP BY id HAVING distance <= 500
ORDER by distance ASC