I have this query to be printed in normal SQL query:
Brand::get()->groupBy('name');
print_r(DB::getQueryLog());
but it is displaying only select * from brands ignoring groupBy. Why is it ignored?
You're grouping after executing the query. The correct way to see your desired result:
Brand::groupBy('name')->get();
print_r(DB::getQueryLog());
What you're doing is executing the groupBy on a Collection. This is working also but it's laravel that is grouping and not your database. That's why you won't see the groupBy command in the SQL query.
you can also use
Brand::groupBy('name')->toSql();
This will print sql query.
Related
I'm using entity framework core for mysql, and i've been running a complex linq query which i'm trying to optimise.
I turned on logging in the mysql server to view the resulting queries from the linq queries.
Oddly, none of it made sense as my complex query that joined 5 tables and performed multiple group bys, where, and order by clause was registered in the logs as 5 separate select all columns from table statements.
So, I tried a simple group by statement for one table. The resulting sql log produced "Select all_columns from table_name order by groupbyid".
Can anyone explain what happened here?
Thanks in advance.
More info as requested:
Sql query:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonId);
queryCommand.ToList();
Resulting mysql log after:
SELECT .... [very long list of column names]
FROM TableExtract AS p
ORDER BY p.tableExtractPersonId
I've tried two different entity framework libraries: MySql.Data.EntityFrameworkCore(v8.0.17) and Pomelo.EntityFrameworkCore.MySql (v2.2.20) with the same results. I've tried .net core 3.0 and also received the same results. I'm going to try .net standard next.
Ok. I found it:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonIdinto g select g.Key)
Forces linq to evaluate as a SQL group by. Otherwise apparently it does it's own thing with the group by.
I am trying to convert SQL code to Seqel to run it from my script. How do I convert this:
select code, count(1) as total
from school_districts
group by code order by total desc;
into Sequel? Or, is there a way to pass raw SQL to Sequel? Also the school_districts will be interpolated #{table_name}.
DB[:school_districts].select(:code).group_and_count(:code).reverse_order(:count)
is a Sequel way of executing that query. I did not however alias the count column, but I hope you can do with this.
Even though working in Sequel is preferable as it allows you to change DBMs without changing your code I would prefer you use the fetch method.
You can do it a couple ways:
Use []:
DB["your sql string"]
Use fetch:
DB.fetch("your sql string")
I have an Oracle form which has EXECUTE_QUERY(NO_VALIDATE) in the KEY_EXEQUERY trigger.
I would like to see the what is the query been executed, how can I print the sql query?
In the Pre-Query trigger get the SYSTEM.LAST_QUERY value to get the text of the query.
I am having Illegal use of WHERE expression for the following statement
select dateField from tableName
where dayname(dayofwk(tableName.dateField)) like 'sunday';
Pls help
Anthony is right, of course. However, there are still at leat 2 options to acheive the same result.
Create a new integer field in your table. This field should store the return value of the dayOfWk() function. Later you can easily query this table.
Second option - create a View inside AX and use a computed column feature toghether with the datePart() SQL Server function. Something like datepart(dw, tableName.DateField) should do it.
The first option will probably result in better performance.
You cannot use a function in a where clause, or in any select statement
you can't use any function on any field of the same table for which you are using the query
How can the following be done with a linq statement?
SELECT Description
FROM Production.ProductDescription
WHERE FREETEXT(Description, 'Some Keywords')
No, the full text search function FREETEXT in TSQL is not directly accessible with Linq to SQL.
You would have to execute that query directly in a database function, then you can pull your result set back with Linq to SQL.
I am not sure, but you might have to search each column/property of the table against your keyword to mimick freetext.
ex:
context.Production.ProductDescription
.Where(pd=>pd.Property1.Contains("Keyword") || pd.Property2.Contains("Keyword");