How do I make 'datetime' column ordered correctly? - mysql

I have a column, it's time is 'DATETIME'. I want to output it in the order of timeline, but when I use ORDER BY create_time DESC, '2011-09-10 16:16:04' is behind '2011-07-16 03:20:01'.
Seems it is treating datatime column as string.
How can I handle this without changing the column type?
UPDATE:
This is part of my query:
SELECT 'bookcomment' AS type
,b.book_id
,b.name
,c.book_id
,c.author_id
,c.content
,c.create_time as create_time
,u.id
,u.name
FROM tbl_book AS b,
tbl_book_comment AS c,
tbl_user AS u
WHERE u.id=c.author_id in (1,2) AND b.book_id=c.book_id
UNION ALL
SELECT 'bookreview' AS type
,b.book_id
,b.name
,re.book_id
,re.author_id
,re.content
,re.create_time as create_time
,u.id
,u.name
FROM tbl_book AS b,
tbl_book_review AS re,
tbl_user AS u
WHERE u.id=re.author_id in (1,2) AND b.book_id=re.book_id
UNION ALL
...
ORDER BY create_time DESC
And part of the output:
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "Whatever" ["author_id"]=> string(4) "test" ["content"]=> string(19) "2011-07-16 03:20:01" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(13) "sdfwefwaefwea" ["content"]=> string(19) "2011-05-11 03:33:33" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test0" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test1" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test2" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test3" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test4" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }

You appear to have the data in the wrong columns. In your output sample create_time is 3 for all of them, so the order is undefined. Double-check data is in the right columns?

try using
ORDER BY CONVERT (create_time, DATETIME) DESC
BUT beware that this costs some performance... changing the column type is IMHO the better option !

Related

Yii2 - Not able to receive data from model with conditional query

I am executing below query:
$model = ProjectSites::find()->alias('ps')->select(['ps.site_id', 'ps.id', 'c.circle_name', 'iss.issue', 'iss.created_at', 'iss.id'])
->join('INNER JOIN', 'tbl_circle c', 'c.id=ps.circle_id')
->join('INNER JOIN', 'tbl_site_issue iss', 'iss.site_id=ps.id')
->where(['ps.delete_flag' => '0'])
->andWhere("ps.circle_id in ($circle_ids)");
if($project_ids) {
$model->andWhere("ps.project_id in ($project_ids)");
}
if($date_range) {
$date = explode('-', $date_range);
$from_date = date('Y-m-d 00:00:00', strtotime($date[0]));
$to_date = date('Y-m-d 23:59:59', strtotime($date[1]));
$model->andWhere("ps.created_at >= '$from_date'");
$model->andWhere("ps.created_at <= '$to_date'");
}
$model->orderBy(['c.id' => SORT_DESC]);
$model->asArray()->all();
var_dump($model); die;
When I generate the SQL query and execute in Phpmyadmin, it returns the data, but I execute the above code, I get below result:
object(yii\db\ActiveQuery)#129 (28) { ["sql"]=> NULL ["on"]=> NULL ["joinWith"]=> NULL ["select"]=> array(6) { [0]=> string(10) "ps.site_id" [1]=> string(5) "ps.id" [2]=> string(13) "c.circle_name" [3]=> string(9) "iss.issue" [4]=> string(14) "iss.created_at" [5]=> string(6) "iss.id" } ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> array(1) { ["ps"]=> string(17) "tbl_project_sites" } ["groupBy"]=> NULL ["join"]=> array(2) { [0]=> array(3) { [0]=> string(10) "INNER JOIN" [1]=> string(12) "tbl_circle c" [2]=> string(17) "c.id=ps.circle_id" } [1]=> array(3) { [0]=> string(10) "INNER JOIN" [1]=> string(18) "tbl_site_issue iss" [2]=> string(17) "iss.site_id=ps.id" } } ["having"]=> NULL ["union"]=> NULL ["params"]=> array(0) { } ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } ["where"]=> array(3) { [0]=> string(3) "and" [1]=> array(1) { ["ps.delete_flag"]=> string(1) "0" } [2]=> string(19) "ps.circle_id in (4)" } ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> array(1) { ["c.id"]=> int(3) } ["indexBy"]=> NULL ["emulateExecution"]=> bool(false) ["modelClass"]=> string(40) "app\modules\projects\models\ProjectSites" ["with"]=> NULL ["asArray"]=> bool(true) ["multiple"]=> NULL ["primaryModel"]=> NULL ["link"]=> NULL ["via"]=> NULL ["inverseOf"]=> NULL }
I want to generate the results of model but I get an object of query.
Thanks.
Do this:
$result = $model->asArray()->all();
var_dump($result); die;

Mysql get all data from table and count of some columns

have table table 1 structure like:
`id, rma_id, order_id, product_id, order_nr, comment, admin_comment, add_date`
and data of it:
(40, 1, 110331, 4399, 1, 'comment1', '', '2016-09-30 08:46:54'),
(42, 2, 110331, 4399, 1, 'comment2', '', '2016-09-30 11:18:06'),
(43, 3, 110374, 4399, 1, 'comment3', '', '2016-10-03 05:55:25'),
(44, 4, 110374, 4399, 1, 'comment4', '', '2016-10-03 05:55:43'),
(45, 4, 110374, 4399, 2, 'comment5', '', '2016-10-03 05:55:43');
Using query like this to get all that of exact RMA:
SELECT * FROM `rma_products` WHERE `rma_id` = 4
and getting result like this:
[0] => array(8) {
["id"] => string(2) "44"
["rma_id"] => string(1) "4"
["order_id"] => string(6) "110374"
["product_id"] => string(4) "4399"
["order_nr"] => string(1) "1"
["comment"] => string(16) "comment4"
["admin_comment"] => string(0) ""
["add_date"] => string(19) "2016-10-03 08:55:43"
}
[1] => array(8) {
["id"] => string(2) "45"
["rma_id"] => string(1) "4"
["order_id"] => string(6) "110374"
["product_id"] => string(4) "4399"
["order_nr"] => string(1) "2"
["comment"] => string(7) "comment5"
["admin_comment"] => string(0) ""
["add_date"] => string(19) "2016-10-03 08:55:43"
}
but I want to add one more field like count that my result would be like this:
[0] => array(8) {
["id"] => string(2) "44"
["rma_id"] => string(1) "4"
["order_id"] => string(6) "110374"
["product_id"] => string(4) "4399"
["order_nr"] => string(1) "1"
["comment"] => string(16) "comment4"
["admin_comment"] => string(0) ""
["add_date"] => string(19) "2016-10-03 08:55:43"
["count"] => string(1) "2"
}
[1] => array(8) {
["id"] => string(2) "45"
["rma_id"] => string(1) "4"
["order_id"] => string(6) "110374"
["product_id"] => string(4) "4399"
["order_nr"] => string(1) "2"
["comment"] => string(7) "comment5"
["admin_comment"] => string(0) ""
["add_date"] => string(19) "2016-10-03 08:55:43"
["count"] => string(1) "2"
}
which would be counted by how many same product_id is in same rma_id
select t.*,s.cnt from t
join
(select rma_id,product_id,count(*) cnt from t where rma_id = 4 group by rma_id,product_id) s
on s.rma_id = t.rma_id and t.product_id = s.product_id

Return the SUM of column in JOIN query

I have a query with a number of joins, I want to return the sum of a column from the JOIN reviews results... any ideas?
SELECT *,user.id AS u_id
FROM user_connects
JOIN user ON user_connects.user_y = user.id
JOIN reviews ON user_connects.user_y = reviews.user
WHERE user_connects.user_x = 1
LIMIT 25
What I want to return something like this, with [1] index totalcolumls being the SUM result of the reviews table:
array(1) {
[0]=>
object(stdClass)#26 (12) {
["id"]=>
string(1) "2"
["user_x"]=>
string(1) "1"
["user_y"]=>
string(1) "2"
["date_created"]=>
string(10) "1457593440"
["first_name"]=>
string(6) "Donald"
["last_name"]=>
string(5) "Trump"
["age"]=>
string(2) "68"
["gender"]=>
string(4) "Male"
["img_url"]=>
string(10) "donald.jpg"
["keto_score"]=>
string(1) "5"
["date_created_dt"]=>
string(19) "2016-03-09 07:00:00"
["u_id"]=>
string(1) "2"
},
[1]=>
object(stdClass)#26 (12) {
["totalcolumls"]=>
string(1) "2"
}
}
you can try like this
SELECT count(reviews.*) AS total_review_column, user_connects.*, reviews.*, user.*, user.id AS u_id
FROM user_connects
JOIN user ON user_connects.user_y = user.id
JOIN reviews ON user_connects.user_y = reviews.user
WHERE user_connects.user_x = 1
group by reviews.user

Mysql variable count not increasing

I have the following MySQL query:
SET #sum1 = 0;
SELECT (#sum1 := #sum1 + 1),date_clicked
FROM `documents_clicks_or_downloads` dclick
JOIN `documents_repository` drep ON dclick.documentID = drep.ID
WHERE accountID = 2304
ORDER BY dclick.ID ASC
Here are the results I get:
"(#sum1 := #sum1 + 1)" "date_clicked"
"1" "2014-04-04"
"2" "2014-04-04"
"3" "2014-04-04"
"4" "2014-04-04"
"5" "2014-04-04"
"6" "2014-04-04"
"7" "2014-04-04"
"8" "2014-04-04"
"9" "2014-04-04"
"10" "2014-04-04"
"11" "2014-04-04"
"12" "2014-04-04"
"13" "2014-04-04"
"14" "2014-04-04"
"15" "2014-04-04"
"16" "2014-04-04"
"17" "2014-04-04"
"18" "2014-04-04"
"19" "2014-04-04"
"20" "2014-04-04"
"21" "2014-04-04"
"22" "2014-04-04"
"23" "2014-04-04"
"24" "2014-04-04"
"33" "2014-04-04"
"34" "2014-04-04"
"35" "2014-04-04"
"36" "2014-04-04"
"37" "2014-04-04"
"38" "2014-04-04"
"39" "2014-04-04"
"40" "2014-04-04"
"41" "2014-04-04"
"42" "2014-04-04"
"43" "2014-04-04"
"44" "2014-04-04"
"45" "2014-04-04"
"46" "2014-04-04"
"47" "2014-04-04"
"48" "2014-04-04"
"49" "2014-04-04"
"50" "2014-04-04"
"51" "2014-04-04"
"52" "2014-04-04"
"53" "2014-04-04"
"54" "2014-04-04"
"25" "2014-04-04"
"26" "2014-04-04"
"27" "2014-04-04"
"55" "2014-04-04"
"56" "2014-04-04"
"57" "2014-04-04"
"28" "2014-04-04"
"29" "2014-04-05"
"58" "2014-04-05"
"30" "2014-04-06"
"31" "2014-04-06"
"59" "2014-04-06"
"61" "2014-04-07"
"62" "2014-04-07"
"63" "2014-04-07"
"64" "2014-04-07"
"65" "2014-04-07"
"66" "2014-04-07"
"67" "2014-04-07"
"68" "2014-04-07"
"69" "2014-04-07"
"70" "2014-04-07"
"71" "2014-04-07"
"72" "2014-04-07"
"73" "2014-04-07"
"74" "2014-04-07"
"75" "2014-04-07"
"76" "2014-04-07"
"77" "2014-04-07"
"87" "2014-04-07"
"88" "2014-04-07"
"89" "2014-04-07"
"90" "2014-04-07"
"91" "2014-04-07"
"92" "2014-04-07"
"93" "2014-04-07"
"94" "2014-04-07"
"95" "2014-04-07"
"96" "2014-04-07"
"97" "2014-04-07"
"98" "2014-04-07"
"99" "2014-04-07"
"100" "2014-04-07"
"101" "2014-04-07"
"102" "2014-04-07"
"103" "2014-04-07"
"104" "2014-04-07"
"105" "2014-04-07"
"106" "2014-04-07"
"107" "2014-04-07"
"78" "2014-04-07"
"79" "2014-04-07"
"108" "2014-04-07"
"109" "2014-04-07"
So the question is, why am I not getting a correct numeric increase on the sum column? It seems to jump all over the place and I can't tell why. Thanks.
The problem is that the select happens before the sorting.
Try doing it as a subquery instead:
SET #sum1 = 0;
SELECT (#sum1 := #sum1 + 1), t.*
FROM (
SELECT
date_clicked
FROM `documents_clicks_or_downloads` dclick
JOIN `documents_repository` drep ON dclick.documentID = drep.ID
WHERE accountID = 2304
ORDER BY dclick.ID ASC
) t

Laravel 5 eloquent many-to-many relationship issue

I'm creating a blog using Laravel 5 but I'm having a strange issue with a many-to-many (via pivot table) relationship.
My DB setup is the following:
posts table:
id | bigint | 20 | PK | Not Null | Auto Increment
title | varchar | 255 | Not Null
body | text | Not Null
category_id | bigint | 20 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null
published | boolean | Not Null
deleted_at | timestamp | Not Null
tags table:
id | bigint | 20 | PK | Not Null | Auto Increment
name | varchar | 255 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null
post_tag pivot table:
id | bigint | 20 | PK | Not Null | Auto Increment
post_id | bigint | 20 | Not Null
tag_id | bigint | 20 | Not Null
created_at | timestamp | Not Null
updated_at | timestamp | Not Null
Following are extracted from my models:
Post
public function tags(){
return $this->belongsToMany('App\Tag');
}
Tag
public function posts(){
return $this->belongsToMany('App\Post');
}
Now on the view if I do this:
var_dump($post->tags->first()->name);
I'm returned the correct tag associated to the post, but obviously the post can have more tags so what I'm trying to do is a for each loop on $post->tags.
Anyway if I try to do it, it seems the result is not usable and the loop never start.
Then I've tried to look at what I'm returned doing this:
var_dump($post->tags);
And I'm returned the following:
object(Illuminate\Database\Eloquent\Collection)#199 (1) {
["items":protected]=> array(1) { [0]=> object(App\Tag)#200 (21) {
["table":protected]=> string(4) "tags" ["fillable":protected]=>
array(1) { [0]=> string(4) "name" } ["connection":protected]=> NULL
["primaryKey":protected]=> string(2) "id" ["perPage":protected]=>
int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true)
["attributes":protected]=> array(4) { ["id"]=> string(1) "2"
["name"]=> string(3) "Tag" ["created_at"]=> string(19) "2015-02-25
15:26:29" ["updated_at"]=> string(19) "2015-02-25 15:26:29" }
["original":protected]=> array(6) { ["id"]=> string(1) "2" ["name"]=>
string(3) "Tag" ["created_at"]=> string(19) "2015-02-25 15:26:29"
["updated_at"]=> string(19) "2015-02-25 15:26:29" ["pivot_post_id"]=>
string(1) "6" ["pivot_tag_id"]=> string(1) "2" }
["relations":protected]=> array(1) { ["pivot"]=>
object(Illuminate\Database\Eloquent\Relations\Pivot)#193 (24) {
["parent":protected]=> object(App\Post)#198 (22) {
["table":protected]=> string(5) "posts" ["dates":protected]=> array(1)
{ [0]=> string(10) "deleted_at" } ["fillable":protected]=> array(4) {
[0]=> string(5) "title" [1]=> string(4) "body" [2]=> string(11)
"category_id" [3]=> string(9) "published" } ["connection":protected]=>
NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=>
int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true)
["attributes":protected]=> array(8) { ["id"]=> string(1) "6"
["title"]=> string(4) "Test" ["body"]=> string(24) " Test
" ["category_id"]=> string(1) "1" ["created_at"]=> string(19)
"2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25
15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL }
["original":protected]=> array(8) { ["id"]=> string(1) "6" ["title"]=>
string(4) "Test" ["body"]=> string(24) " Test
" ["category_id"]=> string(1) "1" ["created_at"]=> string(19)
"2015-02-25 15:49:20" ["updated_at"]=> string(19) "2015-02-25
15:49:20" ["published"]=> string(1) "1" ["deleted_at"]=> NULL }
["relations":protected]=> array(2) { ["likes"]=>
object(Illuminate\Database\Eloquent\Collection)#197 (1) {
["items":protected]=> array(0) { } } ["tags"]=> RECURSION }
["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) {
} ["appends":protected]=> array(0) { } ["guarded":protected]=>
array(1) { [0]=> string(1) "" } ["casts":protected]=> array(0) { }
["touches":protected]=> array(0) { } ["observables":protected]=>
array(0) { } ["with":protected]=> array(0) { }
["morphClass":protected]=> NULL ["exists"]=> bool(true)
["forceDeleting":protected]=> bool(false) } ["foreignKey":protected]=>
string(7) "post_id" ["otherKey":protected]=> string(6) "tag_id"
["guarded":protected]=> array(0) { } ["connection":protected]=> NULL
["table":protected]=> string(8) "post_tag" ["primaryKey":protected]=>
string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=>
bool(true) ["timestamps"]=> bool(false) ["attributes":protected]=>
array(2) { ["post_id"]=> string(1) "6" ["tag_id"]=> string(1) "2" }
["original":protected]=> array(2) { ["post_id"]=> string(1) "6"
["tag_id"]=> string(1) "2" } ["relations":protected]=> array(0) { }
["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) {
} ["appends":protected]=> array(0) { } ["fillable":protected]=>
array(0) { } ["dates":protected]=> array(0) { } ["casts":protected]=>
array(0) { } ["touches":protected]=> array(0) { }
["observables":protected]=> array(0) { } ["with":protected]=> array(0)
{ } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } }
["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) {
} ["appends":protected]=> array(0) { } ["guarded":protected]=>
array(1) { [0]=> string(1) "" } ["dates":protected]=> array(0) { }
["casts":protected]=> array(0) { } ["touches":protected]=> array(0) {
} ["observables":protected]=> array(0) { } ["with":protected]=>
array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }
} }
I've highlighted the part that for me is giving me the problem, for some reason it seems it is having some kind of query recursion which I can't understand why it happens.
Any Idea?
Thanks,
Matteo
You are getting the query collection.
You may convert collection to array using ->toArray() so you can get only the needed data:
$post->tags->toArray();
Maybe your autoload_classmap.php wasn't up to date.
This file is regenerated on each dump-autoload. If you have a new class somewhere in your project it will not be loaded unless it is included in autoload_classmap
composer dump-autoload
This may happened to you, maybe automatically by installing or updating package or something.
Good tip: If you face a problem in Laravel, try to run composer dump-autoload, which often may fix some problems.