I have a library That generates a CRUD with pagination using mysql and created a query with group_by, but the $ this-> db-> count_all_results () is not counted correctly.
I made an adjustment by changing the function of the CodeIgniter for an alternative, using the previous select
select count (*) from ('. $ this-> db-> _compile_select ().') x'
Anyone have other suggestions?
You must especify other params after the count_all() function.
Look this:
$this->db->group_by('my_table_col');
$total = $this->db->count_all('my_table');
Cleber.
Related
I have a table where I need to do two selections. First I need to find OBJ where uunn = abc. Then I need to select where OBJ equals the first result but it doesn't work.
Example:
SELECT OBJ INTO #obj FROM wddt WHERE `uunn`='abc' AND `mqr`='ps';
SELECT mqr FROM wddt WHERE `OBJ` = #obj AND `uunn`='as';
My goal is to check if mqr has a certain content, and I will compare that content in my PHP script.
Multi_query was disabled on the server I was trying to use, just tested everything using XAMPP and works like a charm. I didn't know multi-query could be disabled.
If you don't need the actual results of the first query you may use a subquery in the WHERE clause of the second one:
SELECT mqr FROM wddt WHERE `uunn`='as'
AND `OBJ` LIKE (SELECT OBJ FROM wddt WHERE `uunn`='abc' AND `mqr`='ps');
I'm building some analytical stat graphs for an administrator dashboard using Laravel. I'm having a lot of fun. Though I'm having a bit of difficulty understanding how to utilize the SQL queries and GroupBy function to achieve some stuff.
End goal: retreive the count of users who were created and groupBy the week they registered.
So far I'm using this:
DB::table("users")
->select(DB::raw("COUNT(*) as count"))
->orderBy("created_at")
->groupBy(function ($query)
{
return \Carbon\Carbon::parse($query->created_at)->format("W");
})
->get();
I'm getting an error in Tinker (which is where I do a majority of my testing at the moment it's something like this:
PHP warning: strtolower() expects parameter 1 to be string, object given in
/Users/Ian/sites/bangerz-army/vendor/laravel/framework/src/Illuminate/Database/Grammar.php on line 58
I had gotten a similar query to work in PHP but for performance reasons I'd prefer to keep as much of the math in SQL as possible.
/ Laravel 5.5
Try using DB::raw() in your group by
DB::table("users")
->select(DB::raw("COUNT(*) as count, WEEK(created_at)"))
->orderBy(DB::raw('WEEK(created_at)'))
->groupBy(DB::raw('WEEK(created_at)'))
->get();
I've this line of code
Product::find(['internal_code' => $code])->one();
I was expecting a SQL like this
SELECT * FROM tbl_product
WHERE internal_code = '<code_var_value>'
Instead, debug toolbar show me a flat
SELECT * FROM tbl_product
So, even if internal_code is not found, the result of a select all piped with one() is that the first record of the tabòe (even if not matching) is retrieved.
What am I doing wrong?
Resolved. I misunderstood the documentation.
This is the solution
Product::find()->where(['internal_code' => $code])->one();
In my table I have a field with data such as 1,61,34, and I need to see if a variable is in that.
So far I have this
SELECT id, name FROM siv_forms WHERE LOCATE(TheVariable, siteIds) > 0
Which works, with the exception that if the siteIds were 2,61,53, and TheVariable was 1, it would return the row as there is a 1 in 61. Is there anyway around this using native MySql, or would I need to just loop the results in PHP and filter the siteIds that way?
I've looked through the list of string functions in MySql and can't see anything that would do what I'm after.
Try with find_in_set function.
SELECT id, name FROM siv_forms WHERE find_in_set(TheVariable, siteIds);
Check Manual for find_in_set function.
I cannot figure out how to write the following query using the DbFinderPlugin 1.2.2 with Symfony and Propel:
SELECT species, COUNT(*) FROM Bird GROUP BY species;
Here is the DbFinderPlugin page
I am rather new to the plugin, and I do love it so far, but this query has so far stumped me.
I'm not an expert at DBFinder but it looks like the following should work
$result = DbFinder::from('Bird')->
groupBy('species')->
select(array('species', 'count(*) cnt'))->
find();
Edited to change code
It turns out that you have to use withColumn to get the proper result:
$result = DbFinder::from('Bird')
->withColumn('count(Bird.Id)', 'total_birds')
->groupBy(species')
->find();