having

laravel having: Column not found

扶醉桌前 提交于 2019-11-28 05:45:18
问题 my following code is like this: $places = DivePlace::selectRaw("*,(st_distance_sphere( POINT(".$lon.",".$lat.") , point(lon, lat))/1000) as distance") ->havingRaw("distance < ".$radius) ->orderBy("distance") ->paginate(10); without the "havingRaw" everything is good. After adding it, the following error came up: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from dive_places having distance < 300) Any solution? 回答1: -

Performance implications of allowing alias to be used in HAVING clause

安稳与你 提交于 2019-11-28 00:03:07
I made a bit of a fool out of myself earlier today on this question . The question was using SQL Server, and the correct answer involved adding a HAVING clause. The initial mistake I made was to think that an alias in the SELECT statement could be used in the HAVING clause, which is not allowed in SQL Server. I made this error because I assumed that SQL Server had the same rules as MySQL, which does allow an alias to be used in the HAVING clause. This got me curious, and I poked around on Stack Overflow and elsewhere, finding a bunch of material explaining why these rules are enforced on the

Mysql: how to select groups having certain values?

☆樱花仙子☆ 提交于 2019-11-27 15:17:07
Say there is such table: mysql> SELECT * FROM tags; +---------+--------+ | post_id | tag_id | +---------+--------+ | 1 | 2 | | 1 | 3 | | 1 | 1 | | 2 | 1 | | 2 | 2 | +---------+--------+ 5 rows in set (0.00 sec) Field names are pretty self-explanatory. I want to select post_id s that have both 1 and 3 tag_id s, so in this example it's only 1 . I thought of something like SELECT post_id FROM tags GROUP BY post_id HAVING ... After having I'd like to list tag_id s that are present in this group. How do I do that? rudi-moore If there aren't any unique constraints try: SELECT post_id FROM tags WHERE

PostgreSQL Where count condition

房东的猫 提交于 2019-11-27 14:34:39
I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM "Log" as a, "License" as b WHERE a.license_id=7 AND a.license_id=b.license_id AND b.limit_call > overall_count GROUP BY a.license_id; Why do I get this error: ERROR: column "overall_count" does not exist My table structure: License(license_id, license_name, limit_call, create_date, expire_date) Log(log_id, license_id, log, call_date) I want to check if a license has reached the limit for calls in a specific month. SELECT a.license_id, a.limit_call , count(b.license_id) AS overall_count FROM "License" a LEFT JOIN

Does MySQL eliminate common subexpressions between SELECT and HAVING/GROUP BY clause

烈酒焚心 提交于 2019-11-27 13:48:16
I often see people answer MySQL questions with queries like this: SELECT DAY(date), other columns FROM table GROUP BY DAY(date); SELECT somecolumn, COUNT(*) FROM table HAVING COUNT(*) > 1; I always like to give the column an alias and refer to that in the GROUP BY or HAVING clause, e.g. SELECT DAY(date) AS day, other columns FROM table GROUP BY day; SELECT somecolumn, COUNT(*) AS c FROM table HAVING c > 1; Is MySQL smart enough to notice that the expressions in the later clauses are the same as in SELECT , and only do it once? I'm not sure how to test this -- EXPLAIN doesn't show any

Implementing group_by and having in Laravel using Eloquent

放肆的年华 提交于 2019-11-27 07:59:22
问题 I am having trouble implementing the group_by and having queries using Eloquent in Laravel. Here is the scenario: orders - id - qty deliveries - id - qty - order_id I want to use a join to display the orders with incomplete deliveries as well as the corresponging balance: Order::left_join('deliveries', 'orders.id', '=', 'deliveries.order_id') ->select(array('orders.*'), DB::raw('orders.qty - IFNULL(sum(deliveries.qty),0) AS balance'))) ->group_by('order_id') ->having('balance', '>', 0) ->get(

MySQL - Using COUNT(*) in the WHERE clause

佐手、 提交于 2019-11-27 06:14:43
I am trying to accomplish the following in MySQL (see pseudo code) SELECT DISTINCT gid FROM `gd` WHERE COUNT(*) > 10 ORDER BY lastupdated DESC Is there a way to do this without using a (SELECT...) in the WHERE clause because that would seem like a waste of resources. try this; select gid from `gd` group by gid having count(*) > 10 order by lastupdated desc I'm not sure about what you're trying to do... maybe something like SELECT gid, COUNT(*) AS num FROM gd GROUP BY gid HAVING num > 10 ORDER BY lastupdated DESC SELECT COUNT(*) FROM `gd` GROUP BY gid HAVING COUNT(gid) > 10 ORDER BY lastupdated

Hibernate Criteria API - HAVING clause work arounds

醉酒当歌 提交于 2019-11-27 06:04:40
问题 I've written a query using Hibernate Criteria API to grab a summation of a particular value, now I need to be able to restrict the result to rows where that sum is greater or equal to a particular value. Normally I would use a HAVING clause in my SQL to do this, but the Criteria API doesn't seem to support that at this moment. In raw SQL this is what I need it to do: SELECT user_pk, sum(amount) as amountSum FROM transaction GROUP BY user_pk HAVING amountSum >=50; One work-around that I

Using a HAVING clause in an UPDATE statement

我怕爱的太早我们不能终老 提交于 2019-11-27 05:57:34
问题 This query SELECT FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count' FROM NCAAstats INNER JOIN College_Translator ON College_Translator.AccountID = NCAAstats.AccountId GROUP BY FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, CalendarYear, StatTypeId HAVING COUNT(*) >1 ORDER BY 'Count' DESC Selects records that I would like to set an ISValid bit to 0 . These records are records that appear twice in my database due

PostgreSQL Where count condition

社会主义新天地 提交于 2019-11-26 22:24:33
问题 I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM "Log" as a, "License" as b WHERE a.license_id=7 AND a.license_id=b.license_id AND b.limit_call > overall_count GROUP BY a.license_id; Why do I get this error: ERROR: column "overall_count" does not exist My table structure: License(license_id, license_name, limit_call, create_date, expire_date) Log(log_id, license_id, log, call_date) I want to check if a license has reached the limit for calls in a specific