Symfony combinate 2 queries - mysql

I would like to get max date from other query insert to current query. Look at my codes pls.
Data:
inv_id inv_date inv_export inv_code
1 2016-03-14 0 a2
2 2016-03-13 0 a1
3 2016-04-13 1 a1
4 2016-03-14 0 a1
Result:
for inv_export = 0 return a2 & a1
for inv_export = 1 return a1
mysql (working fine):
SELECT ..., i.inv_date, i.inv_export
FROM Sp.inventory AS i
...
WHERE i.inv_date IN (
SELECT max(i.inv_date) from Sp.inventory WHERE i.inv_export = 1
);
I have tried convert it to symfony:
$qb = $this
->createQueryBuilder('i')
->select('..., i.invDate')
...
->where('i.invExport = :export AND i.invDate = MAX(i.invDate)')
->setParameter('export', $export);

You can use a subquery as follow:
$sub = $this->createQueryBuilder('i2');
$sub->select("max(i2.inv_date)");
$sub->where("i.invExport= i2.invExport");
$qb = $this
->createQueryBuilder('i')
->select('..., i.invDate')
...
->where('i.invDate = ( '.$sub->getDQL().' )');
->andWhere('i.invExport = :export')
->setParameter('export', $export);
hope this help

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)?

MySQL 8 update with Replace() or Regex

How can i update data using replace or regex-like method from
id | jdata
---------------
01 | {"name1":["number","2"]}
02 | {"val1":["number","12"],"val2":["number","22"]}
to
id | jdata
---------------
01 | {"name1":2 }
02 | {"val1": 12,"val2":22 }
I need to make a proper json entry for numbers and replace an array with a number from that array. Column "jdata" can have any number of similar attributes from the example. Something similar to this would do:
UPDATE table SET jdata = REPLACE(jdata, '["number","%d"]', %d);
Two ways:
The long, more clumsy way, using JSON_ARRAY:
UPDATE table1,
(
SELECT
id,
JSON_EXTRACT(jdata, "$.name1[0]") as A,
JSON_EXTRACT(jdata, "$.name1[1]") as B,
JSON_EXTRACT(jdata, "$.val1[0]") as C,
JSON_EXTRACT(jdata, "$.val1[1]") as D,
JSON_EXTRACT(jdata, "$.val2[0]") as E,
JSON_EXTRACT(jdata, "$.val2[1]") as F
FROM table1
) x
SET jdata = CASE WHEN table1.id=1 THEN JSON_ARRAY("name1",x.B)
ELSE JSON_ARRAY("val1",x.D,"val2",F) END
WHERE x.id=table1.id;
Or using JSON_REPLACE:
update table1
set jdata = JSON_REPLACE(jdata, "$.name1",JSON_EXTRACT(jdata,"$.name1[1]"))
where id=1;
update table1
set jdata = JSON_REPLACE(jdata, "$.val1",JSON_EXTRACT(jdata,"$.val1[1]"),
"$.val2",JSON_EXTRACT(jdata,"$.val2[1]"))
where id=2;
see: DBFIDDLE for both options
EDIT: To get more depth in the query, you can start with below, and create a new JSON message from this stuff without the number:
WITH RECURSIVE cte1 as (
select 0 as x
union all
select x+1 from cte1 where x<10
)
select
id,
x,
JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))) j,
JSON_EXTRACT(jdata,CONCAT("$.",JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))))) v,
JSON_UNQUOTE(JSON_EXTRACT(jdata,CONCAT("$.",JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))),"[0]"))) v1,
JSON_UNQUOTE(JSON_EXTRACT(jdata,CONCAT("$.",JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))),"[1]"))) v2
from table1
cross join cte1
where x<JSON_DEPTH(jdata)
and not JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]")) is null
order by id,x;
output:
id
x
j
v
v1
v2
1
0
name1
["number", "2"]
number
2
2
0
val1
["number", "12"]
number
12
2
1
val2
["number", "22"]
number
22
This should take care of JSON message which also contains values like val3, val4, etc, until a maximum depth which is now fixed to 10 in cte1.
EDIT2: When it is just needed to remove the "number" from the JSON message, you can also repeat this UPDATE until all "number" tags are removed (you can repeat this in a stored procedure, I am not going to write the stored procedure for you 😉)
update
table1,
( WITH RECURSIVE cte1 as (
select 0 as x
union all
select x+1 from cte1 where x<10
) select * from cte1 )x
set jdata = JSON_REMOVE(table1.jdata, CONCAT("$.",JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))),"[0]"))
where JSON_UNQUOTE(JSON_EXTRACT(jdata,CONCAT("$.",JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(jdata),CONCAT("$[",x,"]"))),"[0]"))) = "number"
An example, where I do run the update 2 times, is in this DBFIDDLE

Why exactly does this function return a value to some power? (python 2.7)

def power(num, x = 1):
result = 1
for i in range(x):
result = result * num
return result
So I came across a tutorial on calling functions with 2 arguments and this one in the picture was used as an example to show how you could make a function called power(num, x=1) that takes an interval in the first argument and raises it to the power of the second argument. Can someone explain in laymen's terms why this happens and what exactly is going on in this function and 'for' loop?
First, range(x) is equivalent to range(0, x), and generates a sequence that ranges from 0 to x - 1. For example, with range(3) you get the sequence 0, 1, and 2, which has three elements. In general, range(x) generates a sequence that has x elements.
Second, for i in range(x) makes i iterates throught all the elements of range(x). Since range(x) has x elements, i will iterate through x different values, so the statements in the for loop will be executed x times.
With the above analysis, the body of the power function is equivalent to the following:
result = 1
result = result * num
result = result * num
// repeat x times
result = result * num
which is equivalent to:
result = 1 * num * num * ... * num // x nums here
which, apparently, is num raised to the power of x.
Update
Here's how this function works with specific input data. When num is 3 and x is 4, we have:
result = 1
result = result * num // = 1 * 3 = 3
result = result * num // = 3 * 3 = 9
reuslt = result * num // = 9 * 3 = 27
result = result * num // = 27 * 3 = 81 = 3^4
return result // 81 is returned
We can also show the execution process in more details:
result = 1
i = 0 // entering the loop
result = result * num // = 1 * 3 = 3
i = 1 // the second round of the loop begins
result = result * num // = 3 * 3 = 9
i = 2 // the third round of the loop begins
reuslt = result * num // = 9 * 3 = 27
i = 3 // the fourth and final round of the loop begins
result = result * num // = 27 * 3 = 81 = 3^4
// range(4) is exhausted, so the loop ends here
return result // 81 is returned

SQL statement equivalent to ternary operator

I would like to create a statement that is equivalent to (x - y == 0) ? return 0 : return 100 in MySQL. Something that might look like this:
SELECT id, [(integer_val - 10 == 0) ? 0 : 100] AS new_val FROM my_table
I want to compare an attribute in each row to a certain number, and if the difference between that number and the number in the row is 0, I want it to give me 0, otherwise, I want it to give me 100.
Example:
Applying this query on my_table (with 10 being the 'compared to' number):
id | integer_val
===================
1 10
2 10
3 3
4 9
Would return this:
id | new_val
===================
1 100
2 100
3 0
4 0
How can I do this?
Try this:
SELECT id, IF(integer_val = 10, 100, 0) AS new_val
FROM my_table;
OR
SELECT id, (CASE WHEN integer_val = 10 THEN 100 ELSE 0 END) AS new_val
FROM my_table;
Use case when statement:
select *, (case when integer_val = 10 then 100 else 0 end) as New_Val
from yourtable
Try using the IF function:
SELECT id, IF(integer_val - 10 = 0, 0, 100) AS new_val FROM my_table
(I stuck with your condition expression, but it can be simplified a bit since integer_value - 10 = 0 has exactly the same truth value as integer_value = 10.)
Note that the IF function is different from MySQL's IF statement used for stored programs.

php script to move data between columns with mysql

What I want to achieve is to move the data between 2 rows within one table.
Column A
--------
FN2
1 200x310mm
2 400x260mm[+0.84]
3 500x500mm[+11.34]
Column B
--------
0.0000
0.0000
0.0000
0.0000
This is how it should look like after the data move:
Column A
--------
FN2
1 200x310mm
2 400x260mm
3 500x500mm
Column B
--------
0.0000
0.0000
+0.84
+11.34
What I want is that the query between the [ ] is moved to column B and replaces the 0.0000
How can I achieve this?
Kind Regards
just to illustrate what Yadav said
$query = "SELECT columnID, columnA FROM table";
$result = mysql_query($query,$conn);
while ($row = mysql_fetch_array($result)){
$id = $row['columnID'];
$a = $row['columnA'];
$pos1 = strpos($a,"[")+1;
$pos2 = strpos($a,"]");
$b = substr($a,$pos1,$pos2-$pos1);
$query = "UPDATE table SET columnB = $b WHERE columnID = $id";
mysql_query($query,$conn);
}//end while
edit: Yadav obviously proposed a better answer while I was typing mine...
try this it works for you here i used id as unique key ... and test is my database
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("test");
$query=mysql_query("SELECT * FROM test where columnA LIKE '%[%]'");
while($row=mysql_fetch_assoc($query))
{
if(preg_match_all('/\[(.*?)\]/',$row['columnA'],$match))
{
mysql_query("UPDATE test SET columnB='".$match[1][0]."' WHERE id=".$row['id']."");
}
}
?>