PHP MySQL Multi column query - php

I have two tables in the DB
tbl1: Items
Items tbl : id | item_name | item_price | item_qty
Items tbl Row 1: 1 | laptop | 100 | 10
Items tbl Row 2: 1 | television | 80 | 10
Items tbl Row 3: 1 | mobile | 60 | 10
tbl2: Orders
Orders tbl : id | order_num | items_id | items_prices | items+qty | amount
Orders tbl Row1: 1 | OR222879 | ["1,3"] | ["100,60"] | ["10,6"] | 1360;
I can't make a query returns Order's Items Data I'm using Codeigniter.

The more easier and effective solution would be to normalise your data and have each Item ID as separate rows in a different table.
Items - Orders - OrderItems
Orders - id | order_num | total
OrderItems - id | order_num | item_id | quantity
Using this structure you can select all rows using the 'order_num' from the OrderItems table.

Related

JOIN 2 tables based by multiple ids mysql

I have 2 tables let's say orders
id | memberid | productsid |
----------------------------
1 | 23 | 25,27
and products
id | product_name | price |
----------------------------
25 | product1 | 120
27 | product2 | 50
I want to join orders and products table to get product name and price for each id from productsid.
This is the way how I tried to store an order for a member. If you have any better solution, I am waiting to know.
You can join the tables with the help of the function find_in_set(), then group by each order and with group_concat() create a list of the products:
select o.id, o.memberid,
group_concat(p.product_name order by find_in_set(p.id, o.productsid)) products
from orders o inner join products p
on find_in_set(p.id, o.productsid)
group by o.id, o.memberid
See the demo.
Results:
| id | memberid | products |
| --- | -------- | ----------------- |
| 1 | 23 | product1,product2 |

mySQL rank order by sum of values in table that have it name in in another table

I have two mySQL database table, I'm using one to store to product information and the other to store the sales information of product sold, where it receive my product_id as the product it sold
product Table example
product_id | prod_name |
------------------------
1 | toyota |
2 | lexus |
3 | wagon |
sale Table example
sale_id | prod_id | qty
------------------------
1 | 1 | 5
2 | 1 | 1
3 | 3 | 2
4 | 1 | 4
5 | 2 | 5
6 | 2 | 1
Now i want the mysqli datebase to tabulate the sum of the most sold product name using php and html table
you can use this query
SELECT
product.product_id,
product.prod_name,
sum(qty) as total
FROM sale
INNER JOIN product
ON
sale.prod_id=product.product_id
GROUP BY
product.product_id
ORDER BY
total DESC
this fiddle if you want to see the results
and you just need to echo the column name if you want to show it to your table on php

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

Joining 3 Tables, mysql php

How do i join all three tables? I have no idea, because i need to call them all into 1 table
customers
+--------+------------+---------------+---------+---------+
| serial | name | email | address | phone |
+--------+------------+---------------+---------+---------+
| 1 | first_name | email#web.com | address | 7777777 |
+--------+------------+---------------+---------+---------+
orders
+--------+------------+------------+
| serial | date | customerid |
+--------+------------+------------+
| 1 | 2014-03-04 | 1 |
+--------+------------+------------+
order_detail
+---------+-----------+----------+-------+
| orderid | productid | quantity | price |
+---------+-----------+----------+-------+
| 1 | 1 | 30 | 400 |
| 1 | 2 | 10 | 500 |
+---------+-----------+----------+-------+
customerid on table orders are the serial on table customers and orderid on table order_detail are the serial on orders
and what if i use another table? for the productid, where productid = product_id in another table?
help would be much appreciated, I am really sorry for the table, i have no idea how to make tables here but they are in order.
You simply need to use Join statements. So you can do following
Select * from Customers
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
You can also create another table called Product and join the same way as i have showed you.
If you want to select columns from different tables then you have to do like using tablename.columnname. So for example if you want to select quantity and price from order_detail table then do following
Select * from Customers,order_detail.quantity,order_detail.price
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
Hope you got my point.

Categories