Join even on rows that are not in the first table - php

I have two subqueries on which I am doing a join, first one returns following data:
array (size=110)
0 =>
array (size=3)
'scans' => string '7' (length=1)
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
second one:
array (size=21)
0 =>
array (size=2)
'redeems' => string '1' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=3)
'redeems' => string '1' (length=1)
'date' => string '2017-06-15' (length=10)
If I do a left join on those two like so:
LEFT JOIN query2 ON query2.date=query1.date;
I get:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
Row with the date 2017-06-15 gets lost as the second query doesnt have the match with the first.
How can I get both rows returned? Like so:
array (size=110)
0 =>
array (size=4)
'scans' => string '7' (length=1)
'redeems' => '1'
'bonus_points' => string '0' (length=1)
'date' => string '2017-06-13' (length=10)
1 =>
array (size=4)
'scans' => null
'redeems' => '1'
'bonus_points' => null
'date' => string '2017-06-15' (length=10)

Related

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;

fill missing dates for specific key and value

I have an array:
array (size=3)
0 =>
array (size=3)
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (size=3)
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (size=3)
'quantity' => string '15' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '1' (length=1)
how can i fill missing date for each 'eID'? so the output would be:
array (
0 =>
array (
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (
'quantity' => null
'date' => string '2016-02-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
...until 2016-12-01. 12 arrays for each 'eID' key with same value

How to sum data of row has same id

My query :
"SELECT sent_who, sent_amount FROM game1 ORDER BY sent_who ASC;"
Output :
array (size=4)
0 =>
array (size=4)
'sent_who' => string '1' (length=1)
0 => string '1' (length=1)
'sent_amount' => string '1' (length=1)
1 => string '1' (length=1)
1 =>
array (size=4)
'sent_who' => string '2' (length=1)
0 => string '2' (length=1)
'sent_amount' => string '1' (length=1)
1 => string '1' (length=1)
2 =>
array (size=4)
'sent_who' => string '2' (length=1)
0 => string '2' (length=1)
'sent_amount' => string '1' (length=1)
1 => string '1' (length=1)
3 =>
array (size=4)
'sent_who' => string '2' (length=1)
0 => string '2' (length=1)
'sent_amount' => string '1' (length=1)
1 => string '1' (length=1)
What i want :
array (size=4)
0 =>
array (size=4)
'sent_who' => string '1' (length=1)
0 => string '1' (length=1)
'sent_amount' => string '1' (length=1)
1 => string '1' (length=1)
1 =>
array (size=4)
'sent_who' => string '2' (length=1)
0 => string '2' (length=1)
'sent_amount' => string '3' (length=1)
1 => string '1' (length=1)
I want to merge same sent_who in array, also sum sent_amount in same array.
If i use GROUP BY instead of ORDER BY it works for sent_who, but it doesnt give sum of sent_amount
Try this:
SELECT sent_who, SUM(sent_amount) as total_sent_amount
FROM game1
GROUP BY sent_who
ORDER BY sent_who ASC
Using SUM(sent_amount) will sum all the values of sent_amount for the specific sent_who which we grouped using GROUP BY sent_who.
So you will get the array as you expect.

working with join query's result

I have two tables Resources and Categories. I have made a join query and got the result. But I dont know how to work around it. Below is outputted result array.
array (size=4)
0 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '3' (length=1)
'categoryName' => string 'Accounting' (length=10)
'subcategoryID' => string '0' (length=1)
1 =>
array (size=5)
'resourceID' => string '24' (length=2)
'resourceName' => string 'Tafe Resources' (length=14)
'categoryID' => string '4' (length=1)
'categoryName' => string 'Adult Tertiary' (length=14)
'subcategoryID' => string '0' (length=1)
2 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '7' (length=1)
'categoryName' => string 'Automotive' (length=10)
'subcategoryID' => string '0' (length=1)
3 =>
array (size=5)
'resourceID' => string '26' (length=2)
'resourceName' => string 'College Resources' (length=17)
'categoryID' => string '8' (length=1)
'categoryName' => string 'Busniess & Management' (length=21)
'subcategoryID' => string '0' (length=1)
this was my query
$this->db->select()->from('resources');
$this->db->join('categories','resources.resourceID = categories.resourceID');
if it was from single table i will use for-each loop and get data.
I want the duplicated columns to be merged.
I want to display a table where resourceID and resourceName is unique.
How do you want the table to be unique? You could use GROUP BY but you would lose the category information for any duplicates of resourceID and resourceName.
Edit: You can use a JOIN with a GROUP_CONCAT for a subquery but that would limit you to the max length set for the concat operation.
I think the best solution would just to loop through the data and set the data to ensure the rows for resourceID and resourceName are unique.

Return Values of Entire Table Using PDO

I want to run what I thought was going to be a simple query.
I want to return a database table just the values) in the same 2D style it is written in into $data[i][k]
I am using PHP's PDO, and the closest I have been able to get is:
$result=$database->query("SELECT * FROM `garage_statistics`",$bind = null,$fetch = 'FETCH_COLUMN');
$this->statement = $this->pdo->prepare($query);
$result = $this->statement->fetchAll(PDO::FETCH_COLUMN,1);
Which returns
0 => string 'Garage2' (length=1)
1 => string 'Garage3' (length=7)
2 => string 'Garage4' (length=7)
3 => string 'Garage6' (length=7)
4 => string 'Garage7' (length=7)
However I can't get it to loop for all the columns. I tried fetchall and got back:
array (size=10)
0 =>
array (size=14)
'name' => string 't' (length=1)
0 => string 't' (length=1)
'tablename' => string 't' (length=1)
1 => string 't' (length=1)
'numfloors' => string '3' (length=1)
2 => string '3' (length=1)
'status' => string '4' (length=1)
3 => string '4' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:47:13' (length=19)
6 => string '2012-12-07 13:47:13' (length=19)
1 =>
array (size=14)
'name' => string 'Garage 3' (length=8)
0 => string 'Garage 3' (length=8)
'tablename' => string 'Garage3' (length=7)
1 => string 'Garage3' (length=7)
'numfloors' => string '2' (length=1)
2 => string '2' (length=1)
'status' => string '3' (length=1)
3 => string '3' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:49:46' (length=19)
6 => string '2012-12-07 13:49:46' (length=19)
While this does contain all the data, it has so much stuff that I don't want.
Is there a simple way to get a basic 2D array of a table using PDO?
$result = $this->statement->fetchAll(PDO::FETCH_NUM);
Returns correct type

Categories