PHP - I don't want to use table with loops anymore - php

I need to build one table that show all the sales for one day that i've choosen.
I've allways used the dummy way, selecting the table products that had a sale in one day, then do a loop on a php table do get the other informations from others tables.
I wanna know if theres is a simple way getting the data.
TABLES:
table_products
pr_id | pr_name | pr_price | ..
------|---------|----------|
1 | TV | 299.99 |
2 | RADIO | 59.99 |
3 | DVD | 49.99 |
.. | .. | .. |
table_sales
sa_id | sa_coupon | sa_day | sa_month | ..
------|-----------|----------|----------|
1 | 101 | 4 | 9 |
2 | 102 | 5 | 9 |
3 | 103 | 5 | 9 |
.. | .. | .. |.. |
80 | 211 | 2 | 12 |
.. | .. | .. |.. |
table_sales_dt
sa_dt_coupon | sa_dt_product_id | sa_qtd |
-------------|------------------------|-------------|..
101 | 1 | 10 |
101 | 2 | 5 |
102 | 1 | 5 |
103 | 3 | 8 |
211 | 1 | 15 |
211 | 2 | 10 |
211 | 3 | 5 |
.. | .. | .. |
i want to show the results like this:
Sales on month 9 (september)
N PRODUCT QUANTITY PRICE TOTAL
1 - TV - 15 - 299.99 - 4499.85
2 - RADIO - 5 - 59.99 - 299.95
3 - DVD - 8 - 49.99 - 399.92
THANKS!!!

Something like this might do it:
SELECT p.* FROM table_products p
LEFT JOIN table_sales_dt dt ON p.id = dt.product_id
LEFT JOIN table_sales s ON dt.sa_dt_coupon = s.sa_coupon
WHERE s.sa_month = 9

Related

SQL Query - display join result in one result row [duplicate]

This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
MySQL DISTINCT on a GROUP_CONCAT()
(6 answers)
Closed 4 years ago.
I want to create an advanced search based on one table and other tables
my tables:
estate :
+------+-------------+-------------------+-------------------+
| id | title | cat_id | date |
+------+-------------+-------------------+-------------------+
| 1 | test1 | 1 | 1526793203 |
| 2 | test2 | 2 | 1526793203 |
| 3 | test3 | 3 | 1526793203 |
+------+--------------+------------------+-------------------+
estate_risk
+------+-------------+----------------+--------------+
| id | estate_id | title | consequence |
+------+-------------+----------------+--------------+
| 3 | 1 | risktitle1 | 123 |
| 4 | 1 | risktitle2 | 433 |
| 5 | 1 | risktitle3 | 523 |
| 6 | 2 | risktitle4 | 976 |
| 7 | 2 | risktitle5 | 422 |
| 8 | 3 | risktitle6 | 124 |
+------+-------------+----------------+--------------+
related_estate
+------+-------------+----------------+--------------+
| id | estate_id | title | storage |
+------+-------------+----------------+--------------+
| 3 | 1 | testdata | 1 |
| 4 | 1 | testdata2 | 2 |
| 5 | 1 | testdata3 | 3 |
| 6 | 2 | testdata4 | 4 |
| 7 | 2 | testdata5 | 5 |
| 8 | 5 | testdata6 | 6 |
+------+-------------+----------------+---------------+
And some other tables...(foreign all tables is estate_id)
I want to get all the data in a row,in other words for example, I have several related data in the estate_risk table and when I use join query get this result data :
sample query one join:
SELECT R.id,R.title,C.title,C.storage FROM estate R LEFT JOIN estate_risk as C ON estate_risk .estate_id = R.id
result :
----------+------------------+----------------+--------------+
R.id | R.title | C.title | C.consequence
----------+------------------+----------------+--------------+
1 | test1 | risktitle1 | 123 |
1 | test1 | risktitle2 | 433 |
1 | test1 | risktitle3 | 523 |
2 | test2 | risktitle4 | 976 |
2 | test2 | risktitle5 | 422 |
3 | test3 | risktitle6 | 124 |
----------+------------------+----------------+--------------+
Everything is right but i want get all the data in a row that's mean only a row R.id with all C.title in one row
My main goal is to display the page search results just one estate with all other data tables
+------+-------------+----------------+----------------+
id | estate_title| estate_risk | related_estate |
+------+-------------+----------------+----------------+
1 test1 risktitle1 testdata1
risktitle2 testdata2
risktitle3 testdata3
---------------------------------------------------------
2 ...
Maybe my goal is not right, but I do not know how I can get this output And this should be done on the database side, or programming side ?
What ideas can be made on the programming side?
It looks like what you want is a GROUP_CONCAT to put together the risktitles and related_estates.
GROUP_CONCAT(C.title SEPARATOR '\n'),
GROUP_CONCAT(Some_other_column SEPARATOR '\n')
Once you have your group functions, I believe grouping by R.id as Yanet suggested and the data should come together correctly.
If you do have larger data sets though, there are limitations to MySQL's concatenation functions. So in that case you might need to return the data as you are already and then reformat it to match what you want in the PHP side.
Group Concat Limit

How can I fetch the values in MySQL?

I have following table name is sk_event:
|------------------------------------------------------------|
| S.No | ticket_id | event_name | sell_amount | coupon_code |
|------------------------------------------------------------|
| 1 | 5 | Airtel | 450 | ABC |
| 2 | 5 | Airtel | 500 | No Code |
| 3 | 6 | Airtel | 250 | XYZ |
| 4 | 5 | Airtel | 450 | ABC |
| 5 | 6 | Airtel | 250 | XYZ |
| 6 | 5 | Airtel | 450 | ABC |
|------------------------------------------------------------|
Second table i have which name is sk_ticket
|------------------------------------|
| S.No. | Ticket_name | ticket Price |
| 5 | 10 KM | 500 |
| 6 | 5 KM | 300 |
|------------------------------------|
I am using following SQL query :
mysqli_query($con, "SELECT * FROM sk_event e INNER JOIN sk_ticket t ON e.ticket_name = t.ticket_name GROUP BY t.ticket_name") or die(mysqli_error($con));
This query is not giving me the result which I want. I want following answer which will be show in datatable bootstraps. I know how to use datatable bootstraps. I just want child row data.
|-------------------------------------------------------------------|
| # | Ticket Name | base Price | Sell Price | Coupon Used | Total |
|-------------------------------------------------------------------|
| + | 10 KM | 500 | - | - | 4 |
| | 10 Km | - | 450 | ABC | 3 |
| | 10 Km | - | 500 | No Code | 1 |
|-------------------------------------------------------------------|
| + | 5 Km | 300 | - | - | 2 |
| | 5 Km | - | 250 | XYZ | 2 |
|-------------------------------------------------------------------|
So, in the above there are two rows result which is indicate by + and every row have some child rows which bifurcate coupon wise data.
How can I show my data like the above table?
Your tried query will not works because you have tried wrong join query, you should do inner join with primary key and foreign key instead of your tried way. Because joins can perform only with primary key and foreign keys.
You can try below query. Hope below query will work for you and you can get your required data.
select * from sk_event se inner join sk_ticket st on se.ticket_id = st.S.No.

sql order and order_items tables but show shipping charge only once

I have two tables orders and order_items
orders table
+----+-----------------------+
| id | total_shipping_charge |
+----+-----------------------+
| 1 | 60 |
| 2 | 60 |
| 3 | 60 |
| 4 | 60 |
| 5 | 60 |
| 6 | 0 |
| 7 | 60 |
| 8 | 0 |
| 9 | 60 |
| 10 | 60 |
+----+-----------------------+
order_items table
+----+--------------------------+-----+-------+-
| id | order_id | p_name | qty | price |
+----+----------+---------------+-----+-------+---------------------------------
| 1 | 1 | Product 1 | 1 | 699 |
| 2 | 2 | Product 2 | 1 | 349 |
| 3 | 3 | Product 3 | 1 | 199 |
| 4 | 4 | Product x | 2 | 299 |
| 5 | 4 | Product y | 2 | 299 |
| 6 | 4 | Product z | 2 | 299 |
| 7 | 5 | Product 7 | 2 | 299 |
| 8 | 6 | Product 8 | 1 | 1299 |
| 9 | 7 | Product 9 | 1 | 199 |
| 10 | 8 | Product 10 | 6 | 349 |
+----+----------+---------------------------------------------------------------
Now order_id 4 has multiple products in a single order and when I join two tables I get duplicate shipping cost. I want shipping cost on first raw only.
I ran this query
SELECT
o1.id,
o1.total_shipping_charge,
o2.order_id,
o2.p_name
FROM
orders o1
INNER JOIN order_items o2 ON o1.id = o2.order_id
but it shows duplicate row for shipping cost, it should show for first row only.
| id | total_shipping_charge | order_id | p_name
+----+-----------------------+----------+---------+
| 1 | 60 | 1 | Product 1
| 2 | 60 | 2 | Product 2
| 3 | 60 | 3 | Product 3
| 4 | 60 | 4 | Product x
| 5 | 60 | 4 | Product y
| 6 | 60 | 4 | Product z
| 7 | 0 | 6 | Product 7
| 8 | 60 | 7 | Product 8
| 9 | 0 | 8 | Product 9
From Sql Server perspective, what you are asking for is not possible. You are asking from a report point of view which is something different. You can pull this data and on the report show it the way you want but you can't pull the data this way.
Not sure how MySql behaves.
Try
SELECT o1.id,o1.total_shipping_charge, distinct o2.order_id,o2.p_name FROM orders o1 INNER JOIN order_items o2 ON o1.id=o2.order_id

SELECT records WHERE rows have difference on a specific column

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;

PHP and MYSQL select with 5 tables and multiple rows as a single array

Would like to get the following as a result from the table structure below (MYSQL + PHP)
array[0][name]1,[desc]red,[title]hero,[desc]strong,[desc2]smells,[img][0]red1,[img][1]red2,[img][2]red3,ext[0].jpg,[ext][1].gif,[ext][2].png,[count][0]253,[count][1]211,[count][2]21,[count][3]121,[dist][0]5,[dist][1]5,[dist][2]12,[dist][3]2,[score][0]2,[score][1]3,[score][2]1,[score][3]5,[score][4]4,[val][0]5,[val][1]1,[val][2]4,[val][3]3,[val][4]4
The problem I have with a simple SELECT, JOIN and GROUP_CONCAT is that the values duplicate after selecting all the images.
I've tried various other ways for example selecting the data by row combined with a foreach loop in PHP, but I end up with lots of duplicates, and it looks very messy.
I also though about splitting it into multiple selects instead of using one, but I really would like to know if it can be done with one select.
Could someone help me with an MYSQL select? Thanks
game
+-----+----------+
| pid | name |
+-----+----------+
| 1 | red |
| 2 | green |
| 3 | blue |
+-----+----------+
detail
+-----+------+--------+-------+--------+
| id | pid | title | desc | desc 2 |
+-----+------+--------+-------+--------+
| 1 | 1 | hero |strong | smells |
| 2 | 2 | prince |nice | tall |
| 3 | 3 | dragon |big | green |
+-----+------+--------+-------+--------+
image
+-----+-----+-----+----+
| id | pid | img |ext |
+-----+-----+-----+----+
| 1 | 1 | red1|.jpg|
| 2 | 1 | red2|.gif|
| 3 | 1 | red3|.png|
+-----+-----+-----+----+
devmap
+-----+-----+-------+------+
| id | pid | count | dist |
+-----+-----+-------+------+
| 1 | 1 | 253 | 5 |
| 2 | 1 | 211 | 5 |
| 3 | 1 | 21 | 12 |
| 4 | 1 | 121 | 2 |
+-----+-----+-------+------+
stats
+-----+-----+-------+------+
| id | pid | scrore| val |
+-----+-----+-------+------+
| 1 | 1 | 2 | 5 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | 1 | 4 |
| 4 | 1 | 5 | 3 |
| 5 | 1 | 4 | 3 |
+-----+-----+-------+------+
When you do a JOIN that involves more than a 1:1 mapping between tables you're going to have duplicate data, and there's no way to get around that in the query.
You can break it out into multiple selects, or you can loop through the result set and pare out whatever duplicate information you don't want.

Categories