This question already has an answer here:
MySQL JOIN tables with duplicate column names
(1 answer)
Closed 3 years ago.
Right now I have this code:
SELECT * FROM orders, products WHERE product_id=products.id AND `geleverd` = 0
Wich returns as this:
I also have this code for a button:
echo "<a onclick='return window.confirm(\"Weet je zeker dat ".$row['merk']." ".$row['model']." geleverd is?\")' href='change_order_status.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-ok' style='color:black'></span></a>";
But for some reason id returns a 9 instead of a 1
This is the output of a print of $row
Array
(
[0] => 1
[id] => 9
[1] => 2019-1
[ordernummer] => 2019-1
[2] => 9
[product_id] => 9
[3] => 2
[aantal] => 2
[4] => aaa
[omschrijving] => aaa
[5] => 0
[geleverd] => 0
[6] => 9
[7] => Samsung
[merk] => Samsung
[8] => Tablet S2
[model] => Tablet S2
[9] => 1
[threshold] => 1
)
and:
(
[0] => 3
[id] => 9
[1] => 789
[ordernummer] => 789
[2] => 9
[product_id] => 9
[3] => 10
[aantal] => 10
[4] => test
[omschrijving] => test
[5] => 0
[geleverd] => 0
[6] => 9
[7] => Samsung
[merk] => Samsung
[8] => Tablet S2
[model] => Tablet S2
[9] => 1
[threshold] => 1
)
How can I fix that Id will be 1 for the first row and not 9 and for the second row id will be 3 instead of 9
You need to use Alias for the tables, and combine/rename the columns that you need.
SELECT o.id as orderId, p.id as productId, -all the columns you need with the alias first- FROM orders as o, products as p WHERE o.product_id=p.id AND `geleverd` = 0
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array that looks like this. This is a 2 dimensional array.
$MainArray = Array
(
[0] => Array
(
[Job_Name] => WXYZ
[Quantity] => 1000
[Machine_Name] => Machine1
[Start_Date] => 2014-07-30 00:00:00
[Completion_Date] => 2014-08-02 00:00:00
[Labor] => 4
)
[1] => Array
(
[Job_Name] => ABCD
[Quantity] => 1500
[Machine_Name] => Machine2
[Start_Date] => 2014-08-08 00:00:00
[Completion_Date] => 2014-08-14 00:00:00
[Labor] => 2
)
[2] => Array
(
[Job_Name] => BCDA
[Quantity] => 1200
[Machine_Name] => Machine1
[Start_Date] => 2014-08-02 00:00:00
[Completion_Date] => 2014-08-07 00:00:00
[Labor] => 1
)
)
I want to use this information to create a new 3 dimensional array that looks like this.
$ConvertedArray = Array
(
[Machine1] => Array
(
[0] => Array
(
[Job_Name] => WXYZ
[Quantity] => 1000
[Start_Date] => 2014-07-30 00:00:00
[Completion_Date] => 2014-08-02 00:00:00
[Labor] => 4
)
[1] => Array
(
[Job_Name] => BCDA
[Quantity] => 1200
[Start_Date] => 2014-08-02 00:00:00
[Completion_Date] => 2014-08-07 00:00:00
[Labor] => 1
)
)
[Machine2] => Array
(
[0] => Array
(
[Job_Name] => ABCD
[Quantity] => 1500
[Machine_Name] => Machine2
[Start_Date] => 2014-08-08 00:00:00
[Completion_Date] => 2014-08-14 00:00:00
[Labor] => 2
)
)
)
Please any help on this would be appreciated. I am stuck with something and need to figure out how to create the new array using this original array. So basically I am grouping all the jobs from each machine together and the keys for those jobs depend on how they are in the original array. So if the original array has a job with the key 2 and no other job has a higher key on that machine, then it will become key 0 for that job and create a new key with that machine name.
I really appreciate your help on this.
Use below code:-
$result = [];
foreach($MainArray as $record){
$result[$record['Machine_Name']][] = $record;
}
foreach ($MainArray as $value) {
$name = $value['Machine_Name'];
unset($value['Machine_Name']);
$ConvertedArray[$name][] = $value;
}
I am trying to get a result in view returned as array in controller.This is a result of an api call using the json.
I need to display the results in the view. There is design integrated in view to display it.
How could i do it in CodeIgniter?
Function call:
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
This is the var_dumb result of the function call.
stdClass Object
( [content] => Array
( [0] => stdClass Object
( [id] => 1 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479113014667.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 1 [channelName] => Sports [likes] => 0 [views] => 0
[liveViewers] => 0
)
[1] => stdClass Object
( [id] => 2 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479110563909.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 2 [channelName] => Politics [likes] => 0 [views] => 0
[liveViewers] => 0
)
[2] => stdClass Object
( [id] => 3 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479109601008.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 3 [channelName] => Fun [likes] => 0 [views] => 0
[liveViewers] => 0
)
[3] => stdClass Object
( [id] => 4 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/profiles/pic-130.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 4 [channelName] => Art [likes] => 0 [views] => 0
[liveViewers] => 0
)
) [last] => 1 [totalPages] => 1 [totalElements] => 4
[numberOfElements] => 4 [sort] => [first] => 1
[size] => 10 [number] => 0
)
I am new to the CodeIgniter. Please support with sample code.
if you are gonna call as API, no need to put in view.
Just echo like this in controller's method.
$nearme_values = $this->swsdk->nearMe($lat,$lon,$radius,$page,$size);
echo json_encode($nearme_values);
Displaying in View.
// in Controller.
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
$this->load->view('ur_view_file', $data);
// in view
foreach ($nearme_values->content as $nearme) {
echo $nearme->id .' / '. $nearme->screenShotUrl .' / '. $nearme->type .'<br>';
}
I have my tables
companies (id)
purchase_invoice (id,company_id,date)
items(id,company_id,purchase_invoice_id)
gifted_items(id,company_id,purchase_invoice_id)
rebated_items(id,company_id,purchase_invoice_id)
return_items(id,company_id,purchase_invoice_id)
I need to Query all purchase invoices of company and their associated records in other tables where company_id is equal to (user selected ).
I need all these sub tables result as sub columns (idk if i am correct).
Note : company_id and purchase_invoice_id are FK in all other tables
This is what I am trying
$ledgers = array(
'com' => $data['company_id'],
'sd' => $data['start_date'],
'ed' => $data['end_date']
);
$purchaseObj = new Application_Model_DbTable_Ledgers();
$purchaseRes = $purchaseObj->getPurchaseInvoices($ledgers);
public function getPurchaseInvoices($ledgers) {
$sql = $this->select()
->setIntegrityCheck(false)
->from(array('pi' => $this->_name))
->join(array('c' => 'companies'), 'pi.company_id = c.id', array('c.name as companyName'))
->join(array('it' => 'items'), 'it.purchase_invoice_id = pi.id');
// ->join(array('prm' => 'rebated_purchase_mobiles'), 'prm.company_id = pi.company_id')
// ->join(array('rpm' => 'returned_purchase_mobiles'), 'rpm.company_id = pi.company_id')
// ->join(array('pgm' => 'purchase_gifted_mobiles'), 'pgm.company_id = pi.company_id');
$sql = $sql->where('pi.company_id = ?', $ledgers['com']);
if (count($ledgers)) {
if (Performance_Engine::notEmpty($ledgers['sd']) && $ledgers['sd'] != 'sd') {
$dt = new DateTime($ledgers['sd']);
$sdate = $dt->format('Y-m-d');
$sql = $sql->where('CAST(pi.date AS DATE) >= ?', $sdate);
}
if (Performance_Engine::notEmpty($ledgers['ed']) && $ledgers['ed'] != 'ed') {
$dt = new DateTime($ledgers['ed']);
$edate = $dt->format('Y-m-d');
$sql = $sql->where('CAST(pi.date AS DATE) <= ?', $edate);
}
$sql->group('pi.id');
$sql->order('pi.id ASC');
return $sql->query()->fetchAll();
}
}
I need result like below array
Array (
[0] => Array
(
[id] => 102
[company_id] => 8
[grand_total_price] => 45000
[disc_by_rupees] => 111
[disc_by_percent] => 0
[total_price] => 44889
[pay_amount] => 15000
[total_paid_amount] => 15000
[remaining_amount] => 29889
[bill_no] => Q13
[date] => 2015-01-15
[companyName] => QMobile
[items]=> array(
[0] => array(
'id' => 1
'company_id'=> 8
'purchase_invoice_id '=> 102
'model_id' => 1
'date' => 2015-12-1
'IMEI'=>7654367876
'color' => black
'purchase_price' => 500
'is_sold' => 1
'is_returned'=>1
)
[1] => array(
'id' => 2
'company_id'=> 8
'purchase_invoice_id '=> 102
'model_id' => 3
'date' => 2015-12-1
'IMEI'=>34567890
'color' => white
'purchase_price' => 6500
'is_sold' => 1
'is_returned'=>1
)
)
[gifted_items]=> array(
[0] => array(
'id' => 1
'company_id'=> 8
'purchase_invoice_id '=> 102
)
[1] => array(
'id' => 2
'company_id'=> 8
'purchase_invoice_id '=> 102
)
)
[rebated_items]=> array(
[0] => array(
'id' => 1
'company_id'=> 8
'purchase_invoice_id '=> 102
)
[1] => array(
'id' => 2
'company_id'=> 8
'purchase_invoice_id '=> 102
)
)
[return_items]=> array(
[0] => array(
'id' => 1
'company_id'=> 8
'purchase_invoice_id '=> 102
)
[1] => array(
'id' => 2
'company_id'=> 8
'purchase_invoice_id '=> 102
)
)
)
I hope this is understand. please correct me what i need to do.
All I need to do is purchase invoice and all associated table as column which will contain array of selected records
That is not possible in a query.
I'm trying to return some data from a query in this format (or at least very similar)
Array
(
[0] => Array
(
[id] => 1
[name] => Gym Opening Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
[1] => Array
(
[id] => 1
[name] => Resteruant Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
)
I have this query so far, however, it throws an error Error executing PDO query: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SELECT
otg.groupName,
otg.id,
(SELECT
ot.id,
ot.label,
ot.time
FROM
opening_times ot
WHERE
ot.groupId = otg.id) as times
FROM
opening_time_groups as otg
WHERE
otg.venueId = $venueId
AND otg.active = 1
What have I gotten wrong? Or is this something I simply can't do in 1 query?
You sub query is returning more than one row. A sub query in the SELECT must only return a single row.
However it would seem that you could do this simple with a normal join, then sort the array out in the script that calls it:-
SELECT
otg.groupName,
otg.id,
ot.id,
ot.label,
ot.time
FROM opening_time_groups as otg
LEFT OUTER JOIN opening_times ot
ON ot.groupId = otg.id) as times
WHERE otg.venueId = $venueId
AND otg.active = 1
I'm new to this forum from the standpoint of posting, as this question may show. I'm having some issues with the way I want my data to appear.
So, I have 3 tables (I am only showing the columns that I want):
visitors:
center_id (where the visitor was) | state_id (where they came from)
centers:
center_id | center
states:
state_id | state
here is the query that I have been using
SELECT states.state, visitors.center_id, visitors.state_id, centers.center, COUNT(visitors.state_id) AS totalCount
FROM visitors
LEFT JOIN states ON states.state_id = visitors.state_id
LEFT JOIN centers ON centers.center_id = visitors.center_id
WHERE visitors.vdate = <some date> AND visitors.state_id <> '0'
GROUP BY centers.center, visitors.state_id
This produces the following array:
Array
(
[0] => Array
(
[state] => Connecticut
[location_id] => 1
[state_id] => 8
[center] => Little River
[totalCount] => 1
)
[1] => Array
(
[state] => California
[location_id] => 5
[state_id] => 6
[center] => North Augusta
[totalCount] => 1
)
[2] => Array
(
[state] => Colorado
[location_id] => 5
[state_id] => 7
[center] => North Augusta
[totalCount] => 2
)
[6] => Array
(
[state] => Connecticut
[location_id] => 9
[state_id] => 8
[center] => Santee
[totalCount] => 2
)
[7] => Array
(
[state] => Virginia
[location_id] => 9
[state_id] => 51
[center] => Santee
[totalCount] => 1
)
)
This is what I really want:
Array
(
[Little River] => Array
(
[0] => Array
(
[state] => Connecticut
[state_id] => 8
[totalCount] => 1
)
)
[North Augusta] => Array
(
[0] => Array
(
[state] => California
[state_id] => 6
[totalCount] => 1
)
[1] => Array
(
[state] => Colorado
[state_id] => 7
[totalCount] => 2
)
)
[Santee] => Array
(
[0] => Array
(
[state] => Connecticut
[state_id] => 8
[totalCount] => 2
)
[1] => Array
(
[state] => Virginia
[state_id] => 51
[totalCount] => 1
)
)
)
Ultimately I'm putting this into a table that looks something like this:
__________________
|State | Count|
-------------------
| Santee |
-------------------
| Georgia | 5 |
-------------------
| Alabama | 10 |
-------------------
| North Augusta |
-------------------
| another | 7 |
-------------------
Sorry for being long winded, but this was the only way that I could describe it.
I've also tried breaking it out in php, but I'm probably doing something wrong there too. I can make a table with 3 columns with the center listed with each state, but I'm realliy looking for a row that show the center followed by all of the states and counts for that center and on to the next center.
Any assistance would be appreciated.
A query may only return a two- (or less) dimentional result. You will need to parse this result set to transform it into a tree.
With PHP, there really is no difficulty:
foreach ($rawResultSet as $row) {
$finalResult[$row['center']][] = array(
$row['state'],
$row['state_id'],
$row['totalCount']
);
}