get greatest Id from multiple table and remove duplicate value - mysql

i got some problem obtaining value from my db,this is my first db
ID_HARGA ID_USER ID_BAG_PEMASARAN ID_ITEM HARGA ENTRY_DATE
1 9 1 3 1000000 2015-01-11 09:55:27
2 9 1 5 2000000 2015-01-13 07:19:10
5 9 1 3 3000000 2015-01-13 13:47:32
6 9 1 43 7000000 2015-01-13 13:49:49
13 9 1 50 3000000 2015-01-13 17:56:54
37 9 1 50 100 2015-01-19 09:08:20
this is the second one
ID_ITEM NAMA_ITEM SPEC_ITEM MASA_GARANSI STATUS_ITEM DIR_IMAGE
3 water heater emas bagus sekali selamanya 1
5 water heater tembaga bagus super selamanya 1
43 water hater heater seupo 3 1 water_22.jpg
50 tankkk 50 Liter 1 1 water_2.jpg
i know there is some null value just igniore it, i already did this
public function get_item()
{
//menghitung jumlah varian barang yang ada
$querycounter = $this->db->query('select * from ITEM WHERE STATUS_ITEM = 1');
$counter = $querycounter->num_rows();
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
// $this->db->order_by('ENTRY_DATE','DESC');
//$this->db->limit($counter);
$this->db->where('STATUS_ITEM',1);
//$query = $this->db->query('select from ITEM i join HARGA h on i.ID_ITEM = h.ID_ITEM order by h.ENTRY_DATE ASC');
$query = $this->db->get();
return $query->result_array();
//$this->db->select('nama_item','spec_item');
//$query = $this->db->get('item');
}
what i want is,just ignore dir_image
ID_ITEM NAMA_ITEM HARGA DIR_IMAGE
3 water heater emas 1000000
5 water heater tembaga 3000000
43 water hater 7000000
50 tankk 100
ID_item with biggest number not the old , so everytime i am insert into id_harga with ID_ITEM
the result always the newest HARGA
Thx before

There is a little confession that, do you want to get the maximum hagra, or newest inserted data (identifiable by HAGRA_ID if its auto increment by 1) in Hagra table. Your Code should be like
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,max(HARGA),DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
$this->db->where('STATUS_ITEM',1);
$this->db->group_by('item.item_id');
This will join your tables and select maximum value of Hagra. To use Max function, you need to use group_by(CodeIgniter Documentation) clause, other wise it will show only the single maximum value. Group_by will help to show maximum value of each group (each item);
If you want to get the newest inserted data, you can use the same technique using
$this->db->select('MAX(HAGRA_ID),ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');

Related

How with mysql get the same as with php insert...on duplicate key update multiple rows?

For example have such table (named purchase_invoice_items)
Id
NameOfItem
PurchaseQuantity
PurchaseDate
SoldQuantity
1
x
2
2022-04-01
2
y
11
2022-04-01
3
z
8
2022-05-19
4
x
23
2022-08-19
5
x
15
2022-05-19
And i know that sum of sold quantity for NameOfItem x is 20. Sold 20 units of item x. I want to distribute the sold items between PurchaseQuantity using first-in-first-out method. Want to see table like this
Id
NameOfItem
PurchaseQuantity
PurchaseDate
SoldQuantity
1
x
2
2022-04-01
2
2
y
11
2022-04-01
3
z
8
2022-05-19
4
x
23
2022-08-19
3
5
x
15
2022-05-19
15
Using mysql two queries and php, i can do it in following way.
At first i select necessary data from mysql:
$sql_select_purchase_data = 'SELECT `IdPii`, `PurchasedQuantity`
FROM `purchase_invoice_items` WHERE `NameOfItem` = "x"
ORDER BY `PurchaseDate` ASC;';
Then create sql to update.
$sql_update_sold_quantity = 'INSERT INTO `purchase_invoice_items` (`IdPii`, `SoldQuantity`) VALUES ';
php code to continue creating sql
if( isset($arr_select_purchase_data) ){
$sum_of_sold_quantity = 20;
foreach( $arr_select_purchase_data as $one_arr_select_purchase_data ){
if( $sum_of_sold_quantity > 0 ){
$sql_update_sold_quantity .= '(?,?), ';
$data_update_sold_quantity[] = $one_arr_select_purchase_data['IdPii'];//For 'IdPii'
$data_update_sold_quantity[] = min( $one_arr_select_purchase_data['PurchasedQuantity'], $sum_of_sold_quantity);//For 'SoldQuantity'
$sum_of_sold_quantity = $sum_of_sold_quantity - min( $one_arr_select_purchase_data['PurchasedQuantity'], $sum_of_sold_quantity);
}//if( $sum_of_sold_quantity > 0 ){
else{ break; }
}//foreach(
$sql_update_sold_quantity = rtrim(trim($sql_update_sold_quantity), ','). ' ON DUPLICATE KEY UPDATE `SoldQuantity`= VALUES(`SoldQuantity`);';
But this is waste of resources (if i need to select-update many rows)? Two mysql queries and additionally php code.
Any ideas how can i get the same using only mysql (one mysql query; without php)?

Codeigniter - left join tables

i have a table for my sql like this :
result_podium
id_result_podium
id_race
id_rider
position
point
and
result_dnf
id_result_dnf
id_race
id_rider
So what im trying to do is to join those two table and shown it as one, and here is the result and what i have tried so far :
Attempt 1 :
$this->db->select_sum('result_podium.point');
$this->db->select('riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_podium');
$this->db->join('riders','riders.id_rider = result_podium.id_rider');
$this->db->join('result_dnf','result_dnf.id_rider = riders.id_rider', 'left');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_podium.id_rider','result_dnf.id_rider'));
$query = $this->db->get();
return $query->result();
And the result is something like this :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
it managed to get data from table result_podium but not managed to join data from table result_dnf, and here is my another attempt :
Attempt 2 :
$this->db->select_sum('result_podium.point');
$this->db->select('result_dnf.*, riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_dnf');
$this->db->join('riders','riders.id_rider = result_dnf.id_rider');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->join('result_podium','result_podium.id_rider = riders.id_rider', 'left');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_dnf.id_rider','result_podium.id_rider'));
and here is the result :
No
rider
point
1
A
5
2
D
5
3
B
4
4
CC
4
5
Bri
6
Brum
the point data forBri and Brum, is null because they came from result_dnf, but i did not managed to get Nick data from result_podium.
And here what i was expecting for the data to shown in my view :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
5
Bri
6
Brum
is there a way to join those two table?, anyhelp is really appreciated thank you!.

Showing all components in inventory with total stock

I have a table which shows displays list of all works orders with part numbers and quantities booked in those works orders as stock awaiting polishing.
What I want, is to list all part numbers in order to display total number of stock per part.
The current output is:
=======================================================
**Works Order** | **Part Number** | Stock Awaiting Polishing + other columns
1 | B01 | 5
2 | B012 | 12
3 | B012 | 43
4 | B014 | 32
What I want to Display is:
=======================================================
**Part Number** | Stock Awaiting Polishing
B01 | 5
B012 | 55
BO14 | 32
This may be easy but I'm still learning, can I get some help here?
SELECT
data.WORKS_ORDER1 AS Works_Order,
data.PART_NO1 AS Part_Number,
data.Part_Prim_Desc AS Part_Description,
data.Part_Secd_Desc AS Customer,
data.Qty_Painted,
data.Qty_Processed,
data.Available_Stock AS Stock_Awaiting_Polishing
FROM (
SELECT
wip.WO.WO_No AS [WORKS_ORDER1],
wip.WO.Part_No AS [PART_NO1],
wip.Ops.WO_No AS [WORKS_ORDER2],
production.Part.Part_No AS [PART_NO2],
production.Part.Part_Prim_Desc,
production.Part.Part_Secd_Desc,
wip.WO.Qty_Inc_Scrap AS Qty_Painted,
wip.Ops.Qty_Complete + wip.Ops.Qty_Rejected
+ wip.Ops.Qty_Scrapped AS Qty_Processed,
wip.WO.Qty_Inc_Scrap - (wip.Ops.Qty_Complete
+ wip.Ops.Qty_Rejected + wip.Ops.Qty_Scrapped) AS [Available_Stock]
FROM wip.Ops
INNER JOIN wip.WO ON wip.Ops.WO_No = wip.WO.WO_No
INNER JOIN production.Part ON wip.WO.Part_No = production.Part.Part_No
WHERE wip.WO.WO_Complete = 0 AND wip.WO.No_of_Ops_Completed = 1
AND wip.Ops.Op_No = 20 AND wip.Ops.WC_Code = 'VPO' AND wip.Ops.Completion_Ind_YN = 'N'
GROUP BY wip.WO.WO_No, wip.WO.Part_No,
wip.Ops.WO_No,
production.Part.Part_No,
production.Part.Part_Prim_Desc,
production.Part.Part_Secd_Desc,
wip.WO.Qty_Inc_Scrap,
wip.Ops.Qty_Complete,
wip.Ops.Qty_Rejected,
wip.Ops.Qty_Scrapped
) data
WHERE data.Available_Stock > 0
I'm starting my journey with SQL and the above code most likely could be simplified a lot, but it's all I've got and it works the way I want.
All help appreciated!

I want to OR a column with a number and store it back in same columns based on multiple conditions in Mysql

I have the following table called DETAILS.
if Flag is 1 -->00000001 //(1st LSB is set here)
if flag is 2 -->00000010 //(2nd LSB is set here)
if flag is 3 -->00000011 //(1st,2nd LSB's are set here)
if flag is 5 -->00000101 //(1st,3rd LSB's are set here)
Sample data:
ID NAME FLAG(int) IS_LAST
--------------------------------------
1 sports 5 (0000 0101) 0 //1st,3rd LSB's are set
2 News 11 (0000 1011) 0 //1,2,4 MSB's are set
3 Weather 24 (0001 1000) 1 //4,5 MSB's are set
4 IPL 32 (0010 0000) 0 //6th MSB is set
If 2nd LSB or 6th LSB of FLAG column or IS_LAST=1, then I want to OR the FLAG with 64 (0100 0000) and store the result back in to same FLAG column using UPDATE query.
I want the output like this:
ID NAME FLAG(int) IS_LAST
-------------------------------------------------
1 sports 5 (0000 0101)(Not updated) 0
2 News 75 (0100 1011)(updated) 0
3 Weather 88 (0101 1000)(updated) 1
4 IPL 96 (0110 0000)(updated) 0
My SQL has a bitwise or operator - |, so this is just a straightforward update statement:
UPDATE details
SET flag = flag | 64
WHERE (flag | 2 > 0) OR
(flag | 32 > 0) OR
(is_last = 1)

Weighted rotator table design

I am having a hard time with this very simple project. Maybe it's because it's Monday, I'm not sure.
I have a table that looks like this:
id | weight | hits
------------------------------------------------
1 | 4 | 0
2 | 1 | 0
3 | 2 | 0
Obviously, the hits column will increment by 1 each time that particular record is selected (We will run this update from within our PHP script).
How can I best retrieve these records by their weight? What I mean is that if I ran my query 15 times, I would want the following IDs returned:
1
1
1
1
3
3
2
1
1
1
1
3
3
2
1
We have a simple formula in place that we got online that retrieves a random weighted result, but we aren't running the formula enough to statistically balance out the results, so instead we need to do a simple rotation as described above.
I know that this is a simple problem to solve, but I'm having a hard time coming up with the best way to do it today.
I hope I've been clear enough in my description of the problem.
Unless I'm missing something (it is Monday here too after all), can't you just sort by hits / weight and LIMIT 1?
Run:
SELECT id FROM tbl WHERE weight - hits > 0 ORDER BY weight DESC LIMIT 0,1
With the last result, run:
UPDATE tbl SET hits = hits + 1, total_hits = total_hits + 1 WHERE id = (THE RESULT);
Then run
SELECT (Sum(weight-hits)) FROM tbl
If result = 0
UPDATE tbl SET hits = 0
EDIT:
It's possible to make it with two queries:
$query="SELECT id FROM tbl, weight - hits AS diff WHERE weight - hits > 0 ORDER BY weight DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
$id=mysql_result($result,$i,"id");
$diff =0;
while ($i < $num) {
$diff= $diff + mysql_result($result,$i,"diff");
++$i;
}
if( $diff = 1 and $i = 1);
$query2="UPDATE tbl SET hits = 0, total_hits = total_hits + IF(id='$id',1,0)";
else{
$query2="UPDATE tbl SET hits = hits + 1, total_hits = total_hits + 1 WHERE id = '$id'";
}
mysql_query($query2);