Mysql get all data from table and count of some columns - mysql

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

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;

php json_encode id column to string

My array is :
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada'
],
2 => [
'id' => 'FR',
'text' => 'France'
],
...
]
When I do a json_encode on it, for some reason the id value is 0 through the array:
{"id":0,"text":"United States"},
{"id":0,"text":"Canada"},
{"id":0,"text":"France"}
This only happens if the the column name is id as if json_encode forces ID to be numeric.
Any idea how to use a string in the id column ?
You need commas after all of your 'id' => 'XX' lines. After adding those and running json_encode it worked fine for me, the id's all kept their string values.
Try:
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada',
],
2 => [
'id' => 'FR',
'text' => 'France'
]
];
var_dump(json_encode($response, 20));
RESULT:
{
"0": {"id": "US", "text": "United States"},
"1": {"id": "CA", "text": "Canada"},
"2": {"id": "FR", "text": "France"}
}

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

Eloquent query results limited by relation criteria

I have two tables, affiliates and meetings and I want to build Eloquent query which will list all affiliates without those who have appointed meeting at specified date and time. So, the result should contain all affiliates who don't have any meeting and also those with meetings but not on specified date and time.
Users: id, name, city...
Meetings: id, client, date, time, user_id...
Can it be done without joins or raw sql (whereRaw or DB::raw) and how?
Test data users:
array:3 [▼
0 => array:5 [▼
"id" => 2
"name" => "John"
"city" => "New York"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:34:03"
]
1 => array:5 [▼
"id" => 3
"name" => "Elisabeth"
"city" => "Kansas City"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
2 => array:5 [▼
"id" => 4
"name" => "Teodora"
"city" => "New York"
"created_at" => "-0001-11-30 00:00:00"
"updated_at" => "-0001-11-30 00:00:00"
]
]
Test data meetings:
array:3 [▼
0 => array:8 [▼
"id" => 1
"client" => "George P."
"date" => "2015-05-15"
"time" => "14:00:00"
"approved" => 1
"user_id" => 2
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
1 => array:8 [▼
"id" => 2
"client" => "Jack White"
"date" => "2015-05-15"
"time" => "12:00:00"
"approved" => 1
"user_id" => 2
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
2 => array:8 [▼
"id" => 3
"client" => "Philip B."
"date" => "2015-05-16"
"time" => "16:00:00"
"approved" => 1
"user_id" => 3
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
]
Expected result all users who don't have meeting on 2015-05-15 at 12:00
array:2 [▼
0 => array:5 [▼
"id" => 3
"name" => "Elisabeth"
"city" => "Kansas City"
"created_at" => "2015-02-19 00:26:43"
"updated_at" => "2015-02-19 00:26:43"
]
1 => array:5 [▼
"id" => 4
"name" => "Teodora"
"city" => "New York"
"created_at" => "-0001-11-30 00:00:00"
"updated_at" => "-0001-11-30 00:00:00"
]
]
Any help appreciated.
Assuming your user model has a relation meetings you can do this:
$users = User::whereDoesntHave('meetings', function($q){
$q->where('time', '12:00:00');
})->get();

How do I make 'datetime' column ordered correctly?

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 !