I have a small MySQL database for an online booking calendar. Now I wanted to get some stats out of this calendar. I am adding to each entry in the calendar values (full paid, partly paid and a status (paid, unpaid, reserved, etc)).
I have attached an image of the screenshot. As you can see, there are 4 different custom_attribute_ids. ID 1 is saving the status, ID 2 the full price and ID 3 the price already paid. The column entity_id is saving it together. So e.g. all 4 entries with entity_id 232 belongs together.
I now what to display the following:
1. The Sum of all full prices (so custom_attribute_id 2). This I have done with this code:
$result = mysql_query('SELECT SUM(attribute_value) AS value_sum
FROM custom_attribute_values WHERE custom_attribute_id=2');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
This is working and showing me the sum of all full prices entered in the calender.
With the same code I am also showing the sum of the already partly paid amounts.
But now the problem, I want to show the sum of the attribute_value depending on the status. So the code should summarize all values when the custom_attribute_id=2 AND the attribute_value of the relevant entity_id is "Reserviert".
Would be very nice, if somebody could help me, or at least let me know, if this is possible. I am not able to re-design the database, as this code is given from the calendar system.
Here the db as text:
ID custom_attribute_value_id attribute_value entity_id attribute_category
1124 1 Anfrage 233 1
1125 2 1188 233 1
1126 4 233 1
1127 3 015757817858 233 1
1053 1 Reserviert 232 1
1054 2 1700 232 1
1057 3 017697544266 232 1
1058 4 232 1
1039 2 573 231 1
1040 3 088259216300 231 1
1042 1 Reserviert 231 1
1037 3 0043676845617203 230 1
1045 2 2346,50 230 1
1046 1 Reserviert 230 1
1032 1 Anfrage 229 1
1033 2 474 229 1
1034 3 229 1
1027 1 Anfrage 228 1
1029 3 228 1
1030 2 588,50 228 1
1024 3 01729843043 227 1
1025 1 Reserviert 227 1
1023 2 990 227 1
You need a self join so you can have both status and related full price in the same row for each entity. Basically you take a part of the table with only status data and another part of the table with only full price data and join them on the same entity id. On the resulting set you can easily get the sum while using WHERE to restrict rows to those with 'Reserviert' status.
SELECT SUM(t2.attribute_value) AS value_sum
FROM custom_attribute_values t1 JOIN custom_attribute_values t2
ON t1.entity_id = t2.entity_id
AND t1.custom_attribute_id = 1
AND t2.custom_attribute_id = 2
WHERE t1.attribute_value = 'Reserviert'
In this query attribute_value holds status info for t1 and full price for t2.
This should be
$result = mysql_query('SELECT SUM(attribute_value) AS value_sum
FROM custom_attribute_values
WHERE custom_attribute_id=2 AND entity_id = \'Reserviert\'');
You could use a sub select to get the value like this
SELECT entity AS e,
(SELECT SUM(value) FROM custom_attribute_values WHERE entity = e AND attribute = 2) AS s
FROM custom_attribute_values
WHERE value = 'Reserviert'
Your table and column names weren't clear so I made some up...
The first SELECT picks all the 'Reserviert' entities then the sub select sums the values associated with each picked entity.
This is the data I used, with names serial, attribute, value, entity, x
1124, 1, Anfrage, 233, 1
1125, 2, 1188, 233, 1
1126, 4, , 233, 1
1127, 3, 015757817858, 233, 1
1053, 1, Reserviert, 232, 1
1054, 2, 1700, 232, 1
1057, 3, 017697544266, 232, 1
1058, 4, , 232, 1
1039, 2, 573, 231, 1
1040, 3, 088259216300, 231, 1
1042, 1, Reserviert, 231, 1
1037, 3, 004367684561, 230, 1
1045, 2, 2346.50, 230, 1
1046, 1, Reserviert, 230, 1
1032, 1, Anfrage, 229, 1
1033, 2, 474, 229, 1
1034, 3, , 229, 1
1027, 1, Anfrage, 228, 1
1029, 3, , 228, 1
1030, 2, 588.50, 228, 1
1024, 3, 01729843043, 227, 1
1025, 1, Reserviert, 227, 1
1023, 2, 990, 227, 1
Related
I am trying to show a starting order table for speed climbing competition, for multiple categories (children). PHP/Mysql. There are two lanes that are climbed simuntaniously, A and B. Each competitor climbes both lanes. The starting order should be in starting number order for lane A and for lane B in the same order as Lane A with a stagger of 50%, rounding down where there is an odd number of starters.
Sample: Let's say I have a category with 9 competitors, starting numbers from 101 to 109. The output starting order table should be like:
Lane A Lane B
101 105
102 106
103 107
104 108
105 109
106 101
107 102
108 103
109 104
Any ideas? Query or few loops or...
Thanks
Edit:
Query to get table in order Lane A then Lane B:
create temporary table laneA
select * from competitors where category = 1 order by stNo;
create temporary table laneB
select * FROM competitors where category = 1 order by (case when stNo > (select avg(stNo) from competitors )then 1 else 2 end), stNo;
select 1 as rank, x.* from laneA as x
union ALL
select 2 as rank, y.* from laneB as y
order by rank;
drop temporary table laneA;
drop temporary table laneB;
Put the data in a zero-based array, loop over it. Calculate what index you need to access the corresponding elements to show in the second column:
$data = range(101, 109); // fake data array, [0 => 101, 1 => 102, ...]
$c = count($data);
for($i=0; $i<$c; ++$i) {
echo $data[$i] . ' - ' . $data[ ($i + $c / 2) % $c ] . "<br>\n";
}
Echo the current element in the first column, and the one with the index set off by half of the count of elements, “clamped down” to the actual index range by modulo of the count again. (With an uneven count, the inner calculation results in a float with .5 at the end, but the modulo operation automatically equalizes that.)
Result:
101 - 105
102 - 106
103 - 107
104 - 108
105 - 109
106 - 101
107 - 102
108 - 103
109 - 104
And if you add one more, so you have an even number of items, it’ll be
101 - 106
102 - 107
103 - 108
104 - 109
105 - 110
106 - 101
107 - 102
108 - 103
109 - 104
110 - 105
Basically I am using phpexcel.
I have 3 column in those excel be to gorupping it hierarichal.
Column : delivery_order_tli_id is parent. Column : delivery_order_hanwa_id is child of parent. COlumn : coil_ids is concatenate based child of parent.
This is the data.
In this excel, the data like this :
delivery_order_tli_id delivery_order_hanwa_id coil_id
1 1 108
1 1 114
1 1 116
1 1 120
1 1 123
1 1 130
1 1 163
2 1 113
2 1 115
2 1 117
2 1 119
2 1 129
2 1 131
2 1 161
3 3 171
3 221 2880
3 221 2881
3 221 2887
3 221 2889
3 221 2890
4 4 236
4 4 237
4 4 238
4 4 239
4 4 244
4 4 245
4 5 246
4 4 253
So,
$activeSheetData = $objPhpExcel->getActiveSheet()->toArray(null, true, true, true);
I successfully load those column into array.
I nedd format like this.
delivery_order_tli_id delivery_order_hanwa_id coil_ids
1 1 108, 114, 116, 120, 123, 130, 163
2 1 113, 115, 117, 119, 129, 131, 161
3 3 171
3 221 2880, 2881, 2887, 2889,2890
4 4 236, 237,238,239, 244, 245, 253
4 5 246
Please advise.
You could do something like
$result = array();
foreach ($activeSheetData as $data) {
if (isset($result[$data['delivery_order_tli_id'] . $data['delivery_order_hanwa_id']])) {
$result[$data['delivery_order_tli_id'] . $data['delivery_order_hanwa_id']]['coil_id'][] = $data['coil_id'];
$result[$data['delivery_order_tli_id'] . $data['delivery_order_hanwa_id']]['coil_id_csv'] = join(',', $result[$data['delivery_order_tli_id'] . $data['delivery_order_hanwa_id']]['coil_id']);
} else {
$result[$data['delivery_order_tli_id'] . $data['delivery_order_hanwa_id']] = array(
'delivery_order_tli_id' => $data['delivery_order_tli_id'],
'delivery_order_hanwa_id' => $data['delivery_order_hanwa_id'],
'coil_id' => array($data['coil_id']),
'coil_id_csv' => $data['coil_id']
);
}
}
Demo
This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.
Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo
This question is probably easy to solve, but here it comes.
I would like to get the latest data from each vendor (the latest row inserted into the database for each vendor).
This is some sample data from MySQL:
id vendorId vendor url delay price0 price1 price2 price3 price4 price5 price6 date
1 1 vendor1 vendor1.com 0 12 15 18 14 25 28 25 2012-01-18 09:43:40
2 2 vendor2 vendor2.com 0 12 15 18 14 25 28 25 2012-01-18 09:43:40
3 1 vendor1 vendor1.com 0 15 17 122 12 30 52 53 2012-01-18 10:02:40
4 2 vendor2 vendor2.com 0 13 12 123 16 54 61 91 2012-01-18 10:02:40
I would like the output to be:
id vendorId vendor url delay price0 price1 price2 price3 price4 price5 price6 date
3 1 vendor1 vendor1.com 0 15 17 122 12 30 52 53 2012-01-18 10:02:40
4 2 vendor2 vendor2.com 0 13 12 123 16 54 61 91 2012-01-18 10:02:40
What would the SQL be and how would I put the values into this array?
$vendor = array(
array('vendorId' => '1', 'vendor' => 'vendor1', 'url' => 'vendor1', 'delay' => 0, 'price0' => 12, 'price1' => 15, 'price2' => 26, 'price3' => 14, 'price4' => 25, 'price5' => 64, 'price6' => 512),
array('vendorId' => '2', 'vendor' => 'vendor2', 'url' => 'vendor2', 'delay' => 0, 'price0' => 12, 'price1' => 15, 'price2' => 26, 'price3' => 14, 'price4' => 25, 'price5' => 64, 'price6' => 512)
);
Thanks in advance
Also, I forgot to mention: There will be more than just 2 vendors in the database, so I'd prefer that they'd be picked up per auto.
SELECT max( id ) AS max_id, vendorId, url, delay,
price0, price1, price2, price3, price4, price5, price6, date
FROM `vendors`
GROUP BY vendorId
You updated your question with how you should obtain the resultset into an array.
Refer here: http://php.net/manual/en/function.mysql-fetch-array.php
$row = mysql_fetch_array($result);
You will then be able to add each row as an associative array to another array containing all your vendors, accessible by key/value.
Try this query -
SELECT v1.* FROM vendors v1
JOIN (
SELECT vendorId, MAX(date) date FROM vendors GROUP BY vendorId
) v2
ON v1.vendorId = v2.vendorId AND v1.date = v2.date
Not sure if the syntax is correct, but this is roughly how I would do it.
SELECT
`id`, `vendorId`, `vendor`, `url`, `delay`, `price0`,
`price1`, `price2`, `price3`, `price4`, `price5`,
`price6`, `date`
FROM
`vendor_table_name`
GROUP BY
`vendor_id`
HAVING
`date` = max(`date`)
Try below :
select * from vendor_table_name as ts
left join
(select max(id) as id from vendor_table_name group by vendorId) as tsm
on ts.id=tsm.id
However it has limit after that it will be slow approx after 50K - 80K rows
Best way is to make a view for latest records and make join with view.
like below :
create view latestrecords as
select max(id) as id,vendorId from vendor_table_name group by vendorId
and do join with view :
select * from vendor_table_name as ts
left join
latestrecords as tsm
on ts.id=tsm.id
Try this
SELECT * FROM vendor_table_name ORDER BY date DESC
For the array
$result = (query result)
foreach($result as $r){
$array = array("name" => $r->name, .....);
}