PHP Remove the Second Record Group Name - php

anyone know how to grouping a record with same group but the next row the group name is blank with PHP/MySQL, for example :
Record on the database table :
| id | group_title | item | qty | price
| 1 | G-01 | Mouse | 1 | $5
| 2 | G-01 | Keyboard | 1 | $12
| 3 | G-01 | Keyboard | 1 | $12
| 4 | G-01 | Cable | 1 | $1
| 5 | G-02 | Headset | 1 | $20
And expected result :
| Group | Item Name | Qty | Price/qty | Price Total/item | Price Total/group
| G-01 | Mouse | 1 | $5 | $5 | $30
| | Keyboard | 2 | $12 | $24 |
| | Cable | 1 | $1 | $1 |
| G-02 | Headset | 1 | $20 | $20 | $20
I have tried to do it for many days, but still not solved.
Thank in advance.

You can do it with a simple IF and a uservar like this query:
SELECT
IF(g.gname = #myg, '', #myg:= gname) AS gname,
g.mytext
FROM grouptab g
CROSS JOIN ( SELECT #myg:='') AS init
ORDER BY g.gname;
Sample
MariaDB [test]> select * from grouptab;
+----+-------+---------+
| id | gname | mytext |
+----+-------+---------+
| 1 | G01 | Name 1 |
| 2 | G01 | Name 2 |
| 3 | G02 | Name 88 |
| 4 | G02 | Name 99 |
| 5 | G01 | Name 4 |
+----+-------+---------+
5 rows in set (0.000 sec)
MariaDB [test]> SELECT
-> IF(g.gname = #myg, '', #myg:= gname) AS gname,
-> g.mytext
-> FROM grouptab g
-> CROSS JOIN ( SELECT #myg:='') AS init
-> ORDER BY g.gname;
+-------+---------+
| gname | mytext |
+-------+---------+
| G01 | Name 1 |
| | Name 2 |
| | Name 4 |
| G02 | Name 88 |
| | Name 99 |
+-------+---------+
5 rows in set (0.000 sec)
MariaDB [test]>

Related

Color Coding a Table Based on the Count of a Column in the Table

So I have a table. What it does is it has a count for different users. I want to color code it so that a certain color shows up based on the count for the users. So if the count is 0 it will be green. If the count is 1-2 it will be yellow. If the count is 3+ it will be red. I would like a query that can do this. The current search query for the table is "SELECT * FROM table1 WHERE firstname ='$name'"
+--------------------------------------------------------------------------+
| ID | firstname | lastname | address | count |
| | | | | |
+--------------------------------------------------------------------------+
| 1 | john | doe |james street, idaho, usa | 2 |
| | | | | |
+--------------------------------------------------------------------------+
| 2 | cindy | smith |rollingwood av,lyn, canada| 0 |
| | | | | |
+--------------------------------------------------------------------------+
| 3 | rita | chatsworth |arajo ct, alameda, cali | 1 |
| | | | | |
+--------------------------------------------------------------------------+
| 4 | randy | plies |smith spring, lima, (peru)| 2 |
| | | | | |
+--------------------------------------------------------------------------+
| 5 | Matt | gwalio |park lane, (atlanta), usa | 4 |
| | | | | |
+--------------------------------------------------------------------------+```
Here is the table with the

How to select mysql results?

invoice
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
I want to select all rows except the duplicate BRN. (If there are two/more ge in BRN then it should only select one)
I tried:
SELECT *(DISTINCT BRN) FROM invoice
Expected result:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
Given the following table:
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
with the expected results:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
The difficult part is getting the amount, because it is arbitrary, not to mention that the values in Amnt are pretty much worthless in this result.
If you want distinct BRN, the query would be SELECT DISTINCT BRN FROM invoice
You might even get away with SELECT DISTINCT BRN, Name FROM invoice
An intermediate step would be SELECT BRN,Name FROM invoice GROUP BY BRN, Name
But if you try to include Amnt in the equation, then the query will fail because there's no way for the database to determine which Amnt to show.
So, you could try this kludge:
SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN
No guarantees on which Amnt it will pick up, though.
Hope that helps.
SELECT * FROM invoice WHERE Date >= :fdate GROUP BY BRN
See Here Use GROUP BY in Query with your Conditions

MYSQL select query based on another tables multiple values

I have two tables one for units and one for Amenities
Table units
+---------+---------------------+---------------------+-----------+----------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+----------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
| 3 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 3 | Jones |
| 4 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Apartment | Smith |
+---------+---------------------+---------------------+-----------+----------+
Table Amenities
+---------+-----------+-------------------+
| id | Unit_id | Amenity |
+---------+-----------+-------------------+
| 1 | 1 | Air conditions |
| 2 | 1 | Internet |
| 3 | 1 | Water heaters |
| 4 | 1 | TV |
| 5 | 2 | TV |
| 6 | 2 | pool |
| 7 | 2 | Internet |
| 8 | 3 | Internet |
| 9 | 4 | Internet |
+---------+-----------+-------------------+
i want to select units where it has both TV and Internet
I try
select units.* from units left join Amenities on units.unit_id=Amenities.Unit_id
where Amenities.Amenity='TV' and Amenities.Amenity='Internet'
but not work
DROP TABLE IF EXISTS amenities;
CREATE TABLE amenities
(unit_id INT NOT NULL
,amenity VARCHAR(50) NOT NULL
,PRIMARY KEY(unit_id,amenity)
);
INSERT INTO amenities VALUES
(1,'Air conditions'),
(1,'Internet'),
(1,'Water heaters'),
(1,'TV'),
(2,'TV'),
(2,'pool'),
(2,'Internet'),
(3,'Internet'),
(4,'Internet');
SELECT unit_id
FROM amenities
WHERE amenity IN ('TV','Internet')
GROUP
BY unit_id
HAVING COUNT(*) = 2;
+---------+
| unit_id |
+---------+
| 1 |
| 2 |
+---------+
Obviously, you'd never have a table like the one you describe. Instead, you'd have a table of unit, a table of amenities, and a table which says which amenity belongs to which unit.
SELECT ua.*
FROM units AS ua
INNER JOIN units AS ub ON ua.unit_id = ub.unit_id
INNER JOIN Amenities AS aa
ON ua.unit_id = aa.Unit_id
AND aa.Amenity = 'TV'
INNER JOIN Amenities AS ab
ON ub.unit_id = ab.Unit_id
AND ab.Amenity = 'Internet';
Here's the output generated with your data:
+---------+---------------------+---------------------+-----------+-------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+-------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
+---------+---------------------+---------------------+-----------+-------+

Concatenate and Count the multiple rows in single rows in mysql

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values ​​and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |

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;

Categories