Home » Php » multi join tables in mysql and selecting a polyhedral value in php arrays

multi join tables in mysql and selecting a polyhedral value in php arrays

Posted by: admin February 25, 2020 Leave a comment

Questions:

hello i created a installment system with php now i want to showing all all debtors in a page

you should know i get a lot of information from users. users can be select multi product from my system and then get to pay those.
also i have several way to get payment like POS and cash ets that this ways split with , sign

the payment in my system can get several records inserts from users.

the data in my tables insert like this


$register = [
     array(
'id' => 15,
'abutment_id' => 5,
'name' => 'example1'
),

array(
'id' => 13,
'abutment_id' => 4,
'name' => 'example2'
)

'id' => 10,
'abutment_id' => 9,
'name' => 'example3'
)
];

$courses = [
array(
'id' => 1,
'register_id' => 15,
'price' => 100
),

array(
'id' => 2,
'register_id' => 15,
'price' => 70
),

array(
'id' => 3,
'register_id' => 15,
'price' => 500
),

array(
'id' => 4,
'register_id' => 13,
'price' => 500
),


array(
'id' => 4,
'register_id' => 10,
'price' => 100
),

array(
'id' => 4,
'register_id' => 10,
'price' => 500
),
];

$payment = [

array(
'id' => 1,
'abutment_id' => 5,
'course_id' => 1,
'price' => '14,15,15' // every , sing has a way to payment because some user get the money together way
),

array(
'id' => 2,
'abutment_id' => 5,
'course_id' => 1,
'price' => '5,13,1' // every , sing has a way to payment because some user get the money together way
),

array(
'id' => 2,
'abutment_id' => 5,
'course_id' => 3,
'price' => '500,0,0' // every , sing has a way to payment because some user get the money together way
),


array(
'id' => 3,
'abutment_id' => 9,
'course_id' => 3,
'price' => '500,0,0' // every , sing has a way to payment because some user get the money together way
),

array(
'id' => 4,
'abutment_id' => 9,
'course_id' => 3,
'price' => ',0,100,0' // every , sing has a way to payment because some user get the money together way
),


];

i want to doing this job:

first: sum all price of course where all theme owned by one member in register section like this

<h1>example1 have 3 course with price $670</h1> <!-- 3 come from all record in course table with same id and 670 come from sum price in course table with same id in register table -->
<p>this user payment passed `568`</p> <!-- 568 come from all price column of payment table with the same abutment_id in register table. the price sum with every comma sign -->

<h1>example2 have 1 course with price $500</h1>
<p>this user payment passesd 0</p>

<h1>example2 have 1 course with price $600</h1>
<p>this user payment passesd 600</p>

Second: Then user price minus payment and if this is higher than 0 Show like this:

<h2>example1</h2>
<h2>exapmle2</h2>


you can help me with to answer this question in php array or mysql
you should know i have a lot of data in my tables.

I try this way in mysql but cant get result


    SELECT * FROM register_user JOIN deposit JOIN register_user_cource WHERE
register_user_cource.register_user_register_id = register_user.register_user_id AND deposit.deposit_abutment_id = register_user.register_user_abutment_id AND 
SUBSTRING_INDEX(deposit.deposit_price,',',1)
    +
    SUBSTRING_INDEX(SUBSTRING_INDEX(deposit.deposit_price,',',2),',',-1)
    +
     SUBSTRING_INDEX(SUBSTRING_INDEX(deposit.deposit_price,',',3),',',-1)
    +
     SUBSTRING_INDEX(SUBSTRING_INDEX(deposit.deposit_price,',',4),',',-1)
    +
     SUBSTRING_INDEX(SUBSTRING_INDEX(deposit.deposit_price,',',5),',',-1)

     > SUM(register_user_cource.register_user_cource_price)

How to&Answers:

This is SQL for your first point what you want to do.

example1 have 3 course with price $670

SELECT 
    R.name, 
    COUNT(C.id) AS CountCourse, 
    SUM(C.price) AS SumPrice
FROM register AS R
JOIN courses AS C ON C.register_id = R.id
GROUP BY R.name

I need more information about Price column in Payment table. What position is what?