How to count the same values as one sql - mysql

I have a table like this.
----------------------------
| ID | CITY | BLOCK| NAME |
----------------------------
| 1 |Jakarta| A | John |
| 2 |Jakarta| A | Rey |
| 3 |Bekasi | A | Boy |
----------------------------
What's the correct query to count total of blocks where the city is Jakarta and if there are the same blocks in Jakarta, the blocks should be counted as one?
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
-----------------------
If I'm using this query,
SELECT COUNT(block) FROM member AS total_block_jakarta WHERE city = 'Jakarta' GROUP BY block
it will return more than one row like this.
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
| 1 |
-----------------------
If I'm using COUNT(DISTINCT) in above query it will return values like this.
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 2 |
-----------------------
Please help me to find the correct query. Thanks.

try this
SELECT COUNT(DISTINCT block) AS total_block_jakarta FROM MEMBER WHERE CITY = 'Jakarta';

You don't need to group the block since you are referring on how many members are on the Jakarta with the same block.
EDIT:
SELECT COUNT(DISTINCT(block)) FROM member AS total_block_jakarta WHERE kota = 'Jakarta'

SELECT COUNT(DISTINCT BLOCK) AS total_block_jakarta FROM member WHERE CITY LIKE "Jakarta"
Output:
--------------------
total_block_jakarta
1
---------------------

Related

Multiple SELECT statements from different tables

I have two independent tables: 'Clients' and 'Country'.
Country Table:
IdCountry Country
1 SPAIN
2 PORTUGAL
Clients Table
IdClient Entity IdCountry
1 Adam Alves 2
2 Peter Smith 2
3 David Ramos 1
4 Rafael Castro 1
I would like to add a new client into 'Clients' table but using the information from 'Country' table like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT max(IdClient) + 1, '--New--' FROM Clients,
SELECT IdCountry FROM Country WHERE Country = 'SPAIN'
I would like to have this INPUT:
IdClient Entity IdCountry
5 --New-- 1
But if I run this query, it doesn't work. Could anybody help me, please?
COMMENTS: I prefer don't use autoincrement option.
Thank you very much.
Wardiam
You can do it like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT
(SELECT MAX(IdClient) + 1 FROM Clients),
'--New--',
(SELECT IdCountry FROM Country WHERE Country = 'SPAIN')
See the demo.
Results:
| IdClient | Entity | Country |
| -------- | ------------- | ------- |
| 1 | Adam Alves | 2 |
| 2 | Peter Smith | 2 |
| 3 | David Ramos | 1 |
| 4 | Rafael Castro | 1 |
| 5 | --New-- | 1 |

How to count unique items in column in Access

I need help. I can't seem to find the logic behind this code.
I am working on a voting system, and I need to output the results of the votes.
I want to count all of the rows that has a unique name in it and output how many.
My table goes like this.
voterid | pres | vpres | sec | trea | PIO
---------------------------------------------
1 | John | Mitch | James | Jack | Eman
2 | John | Pao | Bryan | Jack | Faye
3 | Kelvin | Pao | James | Jeck | Faye
Output should be
Pres | Votes
--------------
John | 2
Kelvin | 1
Here's my code.
SELECT DISTINCT
pres,
(SELECT COUNT(pres) FROM (SELECT DISTINCT pres FROM tblVote AS Votes)) AS Votes
FROM tblVote
Thanks in advance!
I think you are just looking for a simple GROUP BY query:
SELECT pres, COUNT(*) AS Votes
FROM tblVote
GROUP BY pres

Mysql group-concat is giving wrong output

My purchaseproduct table
+------------+------------+
| productids | quantities |
+------------+------------+
| 1,3,4,5 | 1,1,1,1 |
| 2,3,4,5 | 1,1,1,1 |
+------------+------------+
My product table
productsid | productsname |
+------------+-----------------------------+
| 1 | Phone |
| 2 | Laptop |
| 3 | Charger |
| 4 | Earphone |
| 5 | Camera |
I want to get product name based on productids in purchaseproduct table
Like below Out put is needed
Phone,Charger,Earphone,Camera (In row one)
Laptop,Charger,Earphone,Camera (In row two)
I tried this below statement and many other
select group_concat(p.productsname) from purchaseproducts as pp join products as p on find_in_set(p.productsid,pp.productids);
But the output I get is
Phone,Charger,Earphone,Camera,Laptop,Charger,Earphone,Camera (All in one row)
How can I achieve the output I need?
You can simply use DISTINCT inside the GROUP_CONCAT :
select pp.productsid , group_concat(DISTINCT p.productsname)
from purchaseproducts pp
join products p
on find_in_set(p.productsid,pp.productids);
GROUP BY pp.productsid

Mysql data from table with condition

I have two mysql tables as follows:
contacts
---------------
id | name | email
---------------
1 | Jack | jack#test.com
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
6 | Liz | liz#test.com
7 | Mike | mike#test.com
8 | Jack | jack#test.com
purchases
-------------------
id | contact_id | paid
-------------------
1 | 3 | true
2 | 5 | true
I need unique contact_ids that made purchase and other unique contact_ids that don't have made purchases.
So the final result will be as:
-------------------
id | name | email
------------------
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
7 | Mike | mike#test.com
I tried the query as:
SELECT * FROM contacts LEFT JOIN purchases ON contacts.id = purchases.user_id
But this is not giving me unique rows as required. I tried several combination of DISTINCT, but I am not getting the result as required.
did you try this?
SELECT COALESCE(purchases.contact_id, contacts.id) as id, name, email
FROM contacts
LEFT JOIN purchases ON contacts.id = purchases.user_id
GROUP BY name
SQL FIDDLE
Something like that should work, but its performance is "?".
SELECT * FROM contacts WHERE id IN (
SELECT DISTINCT id as i FROM purchases
UNION
SELECT DISTINCT contact_id as i FROM purchases
)
GROUP BY id

update values from two table with the same pcode and id

I have 2 tables named tbl_sales and tbl_rsales.
Lets assume that i have these ff value for "tbl_sales"
id | pcode | total |
2 | 12345 | 10 |
3 | 12345 | 10 |
Lets assume also that i have these ff value from "tbl_rsales"
id | sales_id | total | pcode |
1 | 1 | 20 | 55555 |
2 | 2 | 10 | 12345 |
3 | 3 | 10 | 12345 |
I can easily update data from "tbl_sales" but my problem is that when i update all value by "pcode" from tbl_sales "tbl_rsales" must be update also. but only those id's from "tbl_sales" that are in "sales_id" from tbl_rsales will update. so in other word. sales_id 1 from "tbl_rsales" will not update only sales_id 2 and 3 will be update because tbl_sales id and tbl_rsales "sales_id" is the same. it's lil complicated for me.any idea is accepted.
UPDATE sales, rsales
SET sales.pcode=rsales.pcode
WHERE sales.id=rsales.id
AND id IN(2,3)
Is this what your looking for?
UPDATE TBL_SALES , TBL_RSALES
SET //WAHTEVER YOU WANT FROM THE TABLE
WHERE TBL_SALES,PCODE = TBL_RSALES.PCODE