mysql price compare, show all prices in a join - php

I have a MySql product table called babyfoontest where all my products are listed like this:
idnummer | ean | typenummer | merk
1 | 1111| Baby1 | Philips
2 | 2222| Baby2 | Alecto
and I have a big price table called prijzen with all prices from different webshops like this:
idnummer | shopnaam | typenummer | price | eancode | producturl | categorie
1 | Amazon | Baby1 | 9,99 | 1111 | www.test.nl| baby
2 | Amazon | Baby2 | 19,99 | 2222 | www.test.nl| baby
3 | BCC | Baby1 | 17,99 | 1111 | www.test.nl| baby
Now on my results page, I want to show 5 products with a price comparison. In my product table, every EAN is unique. I want to match it with all the EAN numbers from the price table and show all prices from min to max. How can I do this?
In the example above if the first product is on the result page, Baby1, I want to prices to show (9,99 AND 17,99) because they match on EAN.
I have this:
SELECT *
FROM babyfoontest
INNER JOIN prijzen
ON babyfoontest.ean = prijzen.eancode
LIMIT 0, 5
So the tables match, but now I get only one price from the prijzen table, and I need all the prices from that product. I use this foreach loop:
$i = 1;
foreach ($data as $info) {
echo $i;
echo ' asd '.$info->typenummer.'<br>';
echo 'asd '.$info->producturl.'<br>';
echo 'asd '.$info->deeplink.'<br>';
echo 'asd '.$info->eancode.'<br><br>';
$i++;
}
Please help me with this!
Thanks!!
Kind regards,
Mark

Perhaps you want to concatenate the prices together. Something like this:
SELECT b.ean, group_concat(p.price order by p.price) as prices
FROM babyfoontest b INNER JOIN
prijzen p
ON b.ean = p.eancode
GROUP BY b.ean
LIMIT 0, 5

Related

Mysql Group By and Sum Total of the column values

enter image description here I am having one table like id, sale_id, item_total, tax fields. need to sum the item_total by grouping the tax values.
Table 1
id | sale_id | item_cost_price | tax |
1 | 10 | 150 | 5 |
2 | 10 | 50 | 7 |
3 | 10 | 30 | 5 |
this is required output:
id | sale_id | item_cost_price | tax |
1 | 10 | 180 | 5 |
2 | 10 | 50 | 7 |
When i tried this query,
SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax
$query=$this->db->query("SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax ");
echo $num = $query->num_rows();
$result=array();
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->item_cost_price;
}
My output is:
i am getting output like this,
am getting distinct tax only. but i need to sum item total values.
Note:
Image 1 : refer my datatable
Image 2: refer my expected outputenter image description here
Add SUM(item_cost_price) in SELECT statement.
In your select statement your select only sale_id, tax. But what you echo are sale_id, tax, and item_cost_price which not exist in your SELECT statement. Try This:-
$sql = "SELECT sale_id,tax, SUM(item_cost_price ) AS TotalPrice FROM bgs_ib_sales_items WHERE sale_id = '10' GROUP BY tax";
$query=$this->db->query($sql);
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->TotalPrice;
}

How to select only one record of each category?

Products :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 3 | B | Dell | 1 |
--------------------------------------------
| 4 | B | Dell | 1 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
| 6 | C | Dell | 1 |
--------------------------------------------
Hi everyone, i have a table (products) stored in MySql with many records, for now i'm using this query SELECT * FROM products WHERE sold = 0, in results i get :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
i want to get only one record from each group, so the results will be like :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
You could easily do this by using a distinct clause and removing the id column. If you want to keep the id column you need to specify how one would chose which id to keep.
select distinct
`group`
, name
, sold
from
products
where
sold = 0;
To keep the row with the smallest id (as your example shows) something along the lines of the example below would work.
select
id
, `group`
, name
, sold
from
products
where
sold = 0
and id = (
select
min(p.id)
from
products p
where
p.`group` = products.`group`
and p.sold = 0
);
First, change your field named Group to something like Group_Name. GROUP is a reserved keyword, and if it is not causing you problems now it probably will later.
Second, you should ask yourself what you are really after. The following query should generate your desired result. It adds an additional condition where the IDs that are returned are the lowest numbered ID in each group.
SELECT * FROM products
WHERE sold = 0
AND ID IN (SELECT MIN(ID) FROM products WHERE sold = 0 GROUP BY Group_Name)
Why do you want that, though? That is not a normal desired end state. You should ask yourself why you care about the ID. It looks like your goal is to figure out which products have not sold anything. In that case, I would recommend this instead:
SELECT DISTINCT Group_Name, Name
FROM products
WHERE sold = 0
ORDER BY Group_Name, Name
I found the solution by using the statement GROUP BY,
SELECT * FROM products WHERE sold = 0 GROUP BY group
in the results now, i get only one record for each group and the minimal id without adding any other statement, and in my real table i am using product_group instead of group because it's a reserved word.
Try this:
SELECT `ID`, `Group`, `Name`, `Sold` FROM products WHERE sold = 0 GROUP BY `Group`;

Get joined datas during a MySQL query with PHP

My environment:
I have these tables:
Table ___Billing:
|----------|----------|----------|--------------|---------------------|
| BIL_Id | BIL_Item | BIL_Rate | BIL_Quantity | BIL_ApplicableTaxes |
|----------|----------|----------|--------------|---------------------|
| 1 | Hot-Dog | 4.50 | 2 | 3 |
| 2 | Tea | 3.25 | 3 | 3,4 |
|----------|----------|----------|--------------|---------------------|
BIL_Id = the ID for this item in this table.
BIL_Item = the id of the item (referring to the ___SalesTaxes table).
BIL_Quantity = the quantity of the item the customer used.
BIL_ApplicableTaxes = the id of the applicable taxes for this item. Each taxe id is comma separated.
Table ___SalesTaxes:
|----------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|----------|--------------|------------|
| 3 | Tax 1 | 3.50 |
| 4 | Tax 2 | 6.55 |
|----------|--------------|------------|
STX_Id = the ID for this item in this table.
STX_TaxeName = the name of the taxe.
STX_Amount = the amount in percentage of the taxe.
My question:
How is it possible from these two tables, to loop into the ___Billing table in order to get the taxes in percentage I need to applied ?
For example :
For the first row, I should have: 3.50.
For the second row, I should have : 3.50, 6.55.
Hope the question is clear.
Thanks.
If you phrase your query correctly, you can use MySQL's FIND_IN_SET() function to match tax IDs agains the CSV list you have in the BIL_ApplicableTaxes column:
SELECT t1.BIL_Id,
t1.BIL_Rate,
GROUP_CONCAT(COALESCE(t2.STX_Amount, 'NA')) AS tax_due
FROM ___Billing t1
LEFT JOIN ___SalesTaxes t2
ON FIND_IN_SET(t2.STX_Id, t1.BIL_ApplicableTaxes) > 0
GROUP BY t1.BIL_Id
However, you should seriously consider normalizing the ___Billing table and removing the CSV data. Instead, each tax entry should have its own record.
Demo here:
SQLFiddle

MySQL Select second count of pre selected results

Hello I am trying to make a Select where the uses chooses department and would like to have clause WHERE this department is first, let's say we select 10 results from department: Taxes and then make a SUM SELECT of fee WHERE status = 1. Which results be selected based on the first select All the results are coming from the same table.
| id | department | status | fee |
----------------------------------
| 1 | tax | 1 | 20 |
| 2 | tax | 2 | 20 |
| 3 | tax | 1 | 20 |
| 4 | accounting | 1 | 20 |
So I would like to select if department is choose as tax, and status is 1 the sum of FEE columns which should be 40
So far my Select query looks like this:
SELECT P.id, P.fee, (SELECT SUM(P.fee) FROM cases P WHERE status = 1) as fee_USD
FROM cases P WHERE 1";
if (!empty($department)) { $sql .= " AND P.department = '$department'"; }
the last line is checking if department is given as select option. there are other options as well but to make it simple I have pasted only this part of it. Any help is welcome.
In the Current Selection Fee is = 80
You have to add correlation to your query:
SELECT P1.id, P1.fee,
(SELECT SUM(P2.fee)
FROM cases P2
WHERE P2.department = P1.department AND status = 1) as fee_USD
FROM cases P1
WHERE 1 ...
This way the subquery will return the SUM of only those records which are related to the current record of the main query.

Group results in subgroups

I have been trying to get the subgroup total of the data below using the method in the accepted answer here: how to group result in subgroups in php, but to no avail. Somebody please help.
I want this:
+---------------+-----------------+
| Type | Price |
+---------------+-----------------+
| Music | 19.99 |
| Music | 3.99 |
| Music | 21.55 |
| Toy | 89.95 |
| Toy | 3.99 |
+---------------+-----------------+
displayed as this:
Music | 45.53
Toy | 93.94
in group and subtotal.
You should do it directly in MySQL when you perform the query (if possible):
SELECT DISTINCT Type, SUM(Price) as Total FROM table_name GROUP BY Type
You could select the sum of the prices, then group the subtotals by type. I used this SQL statement in a PHP script;
SELECT SUM(Price) AS Total, Type FROM Table GROUP BY Type
I then executed the SQL statement and echoed the results in a simple foreach loop. It appears to work fine with me.
Try this...
// Get the sum
$group = array();
foreach($rows as $r){
$group[$r->type] = $group[$r->Type] + $r->Price;
}
// Display
foreach($group as $type=>$price){
echo "Type:".$type." Price: ".$price;
}

Categories