Yii2: findone() syntax on multiple where condition - php

I have a table ipd_charges with columns
table - ipd_charges
id doctor room_category charges_cash charges_cashless
1 1 1 200 300
2 1 2 300 400
table - patient_admission
id patient_name tpa_name(if not null, equivalent to charges_cashless)
1 1 Null
2 2 1
table daily_ward_entry
id patient_name room_name doctor_name charges ( from ipd charges)
1 1 1 1 200
2 2 2 1 400
I am trying to use this query which is failing:
$model = \app\models\IpdCharges::find()
->where(['doctor'=>$id])
->andwhere(['room_category'=>$this->room_name])->one();
Thanks. Please tell me if any more info is needed.
A help will be greatly appreciated.

Please check below code :
$ipdCharges = new IpdCharges();
$ipdCharges->findOne(['doctor' => $id, 'room_category' => $this->room_name]);

Related

array_push and array_search array_splice on Laravel query builder

Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation

Select where rows exist more than once with variation

This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.

How to get price difference from same table in SQL

I have following table
slno date productid companyid price
88 2017-05-17 1 1 65.27
87 2017-05-17 1 2 72.94
86 2017-05-17 1 3 73.13
85 2017-05-17 2 1 73.73
84 2017-05-17 2 2 67.71
83 2017-05-16 1 1 65.40
82 2017-05-16 1 2 72.49
81 2017-05-16 2 1 73.31
80 2017-05-16 2 2 67.17
Now I want price of product 1 for 2017-05-17 and difference of price from yesterday for same company id.
e.g:
getPrice( productid = 1, date='2017-05-17')
and this should return :
companyid , productid , date, price, difference from yesterday:
1, 1,'2017-05-17', 65.27, -0.13
2, 1,'2017-05-17', 72.94, 0.45
...
or it should return:
companyid , productid , date, price, yesterday price:
1, 1,'2017-05-17', 65.27, 65.40
2, 1,'2017-05-17', 72.94, 72.49
...
How to get this in PHP SQL?
You should consider doing this with php, because php can tell you yesterdays and today date. So get yesterdays and today date with php and use it to manipulate the sql query
so the query should look something like this
yesterdays Price:
select companyid, productid , date, price from table where productid='1' and date='$yesterday';
Today price
select companyid, productid , date, price from table where productid='1' and date='$today';
now you can get both values and subtract them with php to get the difference

nested where condition in codeigniter

from this data how can i fetch all users where user_id in tbl_attendance not having current date with application_status 3 and 4
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
my code is like this
$date=date('Y-m-d');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" )');
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
want to combine those two lines and produce the output like if current date is present in 3 or 4 please help me
You can try like this.To combine two conditions
$this->db->where("tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='$date' AND tbl_attendance.attendance_status IN(3,4))");
seeing you're using query builder...
$this->db->group_start()
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
$this->db->where_not_in('tbl_users.user_id', 'SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in = '. $date, false)
$this->db->group_end()
should work

mysql distinct manufacturer,count of 2 other columns and sum of value

Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo

Categories