Mysql select distinct id's and select first row only - php

This is my table. I want to select distinct 'bill_no' with only first related row.
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 2 | 12 | Spiker gel | 10 | 300 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 4 | 13 | test prod | 1 | 145 |
| 5 | 14 | wokk | 3 | 55 |
| 6 | 14 | Kamer hyp | 11 | 46 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
I want data to be displayed like the below table having only distinct "bill_no" -
Output-
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 5 | 14 | wokk | 3 | 55 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+

Use group by
select * from youtable group by bill_no

select t1.*
from your_table t1
join
(
select min(id) as id
from your_table
group by bill_no
) t2 on t1.id = t2.id

You can use GROUP BY clause.GROUP BY clause always return first row with related group by cloumn name.
SELECT * FROM `table_1` group by `bill_no`

Related

How to manage SELECT data from 3 different table's using with JOIN?

I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;

how to calculate unique column values in mysql

Here is my data
cardNo| userName| tablename| hours | date
1 | a | a | 12 | 12-06-2015
1 | a | a | 5 | 11-06-2015
2 | b | b | 3 | 15-06-2015
1 | a | a | 8 | 12-06-2015
2 | b | b | 3 | 21-06-2015
1 | a | a | 12 | 14-06-2015
2 | b | b | 10 | 8-06-2015
cardNo is unique. I need to display all details and total hours for each card, like:
cardNo | userName | tablename | totalhours
1 | a | a | 37
2 | b | b | 16
It's simple SUM() with GROUP BY:
SELECT cardNo,sum(hours)
FROM yourtable
GROUP BY cardNo;
I left it as an exercise for the OP to include userName and tablename columns into the query
SELECT cardNo,userName, tablename, sum(hours) hours
FROM Table_1 GROUP BY cardNo,userName,tablename

MYSQL: Howto get last download from 2 joined tables

This is my first question, so please be nice :)
I have this:
mysql> select cat_id, cat_name from phpbb_dm_eds_cat;
+--------+----------+
| cat_id | cat_name |
+--------+----------+
| 9 | catx |
| 10 | cat2 |
| 11 | test |
+--------+----------+
mysql> select subcat_id, subcat_name from phpbb_dm_eds_subcat;
+-----------+--------------+
| subcat_id | subcat_name |
+-----------+--------------+
| 39 | aaa |
| 40 | xxx111 |
| 41 | TESTXX |
| 42 | xxa |
+-----------+--------------+
Here is the download table:
mysql> select download_id, download_title, download_cat_id from phpbb_dm_eds;
+-------------+----------------+-----------------+
| download_id | download_title | download_cat_id |
+-------------+----------------+-----------------+
| 3 | s | 9 |
| 5 | raver | 41 |
| 6 | hans | 10 |
| 7 | readme | 42 |
+-------------+----------------+-----------------+
Now the query:
mysql>
SELECT bc.cat_name, bc.cat_id,
COUNT(bd.download_id) AS number_downloads,
MAX(bd.last_changed_time) AS last_download
FROM phpbb_dm_eds_cat bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id ;
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 0 | NULL |
+----------+--------+------------------+---------------+
the 'test' category has a subcategory which has a download in the subcat table, the categories 'catx' and 'cat2' have downloads too, there the last download is displayed correctly but i want the last_download in the subcat 'test' to be displayed too
How do I write the query?
You are not clear about what you want but it might be:
SELECT bc.cat_name, bc.cat_id,
COUNT(bd.download_id) AS number_downloads,
MAX(bd.last_changed_time) AS last_download
FROM (
select cat_id, cat_name from phpbb_dm_eds_cat
union
select subcat_id as cat_id, subcat_name as cat_name
from phpbb_dm_eds_subcat
) as bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id ;
(Your SELECT is using a MySQL extension, because cat_id is neither in the GROUP BY nor in an aggregate.)
From your notes"the 'test' category has a subcategory" ,i suggest there should be a relation between cat table "phpbb_dm_eds_cat" and sub_cat table
"phpbb_dm_eds_subcat".
Like this:
Table name
phpbb_dm_eds_cat
Columns
(cat_id,cat_name)
Tablename
phpbb_dm_eds_subcat
Columns
(cat_id referencing table phpbb_dm_eds_cat(cat_id),subcat_id,subcat_name)
Sadly your query doesn't work, it gives me ALL subcats, like this:
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 0 | NULL |
| aaa | 39 | 0 | NULL |
| xxx111 | 40 | 0 | NULL |
| TESTXX | 41 | 1 | 1427123713 |
| xxa | 42 | 1 | 1427136789 |
+----------+--------+------------------+---------------+
7 rows in set (0,00 sec)
I don't want the subcats to be displayed, I only want the last download from the subcat, this should be the correct output:
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 1 | 1427213321 |
+----------+--------+------------------+---------------+
3 rows in set (0,00 sec)

fetch data from multiple tables

I have 4 table for my Ads Portal.
Firstly main table name is ads
+-------------+
| Field +
+-------------+
| id +
| ads_title +
+-------------+
example data for ads table;
+----+-----------------+
| id | ads_title |
+----+-----------------+
| 20 | 2006 CITROEN C4 |
| 27 | EMEKLI OGRETMEN |
| 28 | Harika 10 n |
| 34 | HATASIZ BOYASIZ |
| 49 | BAYANDAN 2009 |
+----+-----------------+
for ads specifications stored in a table. table name is ads_spc
+---------+
| Field |
+---------+
| id |
| key_name|
+---------+
data example for ads_spc table;
+----+-------------+
| id | key_name |
+----+-------------+
| 1 | Date |
| 2 | Km |
| 3 | Colr |
| 4 | Engine |
| 5 | Pw. Engine |
| 6 | Oil |
| 7 | Speed |
| 8 | Boody Type |
| 9 | Traction |
| 10 | Warranty |
+----+-------------+
Lastly stored specification values for ads_spc by ads_id. table name is ads_values
+------------+
| Field |
+------------+
| id |
| ads_id |
| spc_id |
| value |
+------------+
for example this table data
+----+--------+--------+-------+
| id | ads_id | spc_id | value |
+----+--------+--------+-------+
| 25 | 49 | 9 | 2119 |
| 26 | 49 | 10 | 2131 |
| 27 | 34 | 1 | 2010 |
| 28 | 34 | 2 | 8900 |
| 29 | 34 | 3 | 4 |
| 30 | 34 | 4 | 22 |
| 31 | 34 | 5 | 40 |
| 32 | 34 | 6 | 60 |
| 33 | 34 | 7 | 65 |
| 34 | 34 | 8 | 66 |
+----+--------+--------+-------+
I want to using these tables list ads (table: ads) with specifications (table:ads_spc) and ads values (table:ads_values) in a data row.
Union to;
ADS + ADS_SPC + ADS_VALUES (All values searchable)
I'm using this case:
SELECT SQL_CALC_FOUND_ROWS
*
FROM ads AS a
LEFT JOIN ads_spc AS aspc
LEFT JOIN ads_values AS aval ON aval.ads_id=a.id AND aval.spc_id=aspc.spc_id
How can I union to one row?
Sorry for my english.
Why union there is no sense for the union you should join these tables
SELECT v.id, a.ads_title,s.key_name,v.value FROM `ads` a
INNER JOIN `ads_values` v ON (a.id =v.ads_id)
INNER JOIN `ads_spc` s ON(v.spc_id =s.id) WHERE s.key_name='Color'

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