Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Mysql Query Question, MySQL Select query problem
Manu Dhanda
post Jan 20 2008, 01:51 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 1
Joined: 20-January 08
Member No.: 27,758



Hii Guyz,

I am having this little problem creating a query in MySQL.

Problem description:

Table: mytable

Table Structure:

IssueId | ToolName | Status
01 | Tool1 | Open
02 | Tool1 | Closed
03 | Tool2 | Closed
04 | Tool3 | Open

Now, I want to get the results in the following form:

ToolName | Count(Open) | Count(Closed)

For the above input, result will be:

Tool1 | 1 | 1
Tool2 | 0 | 1
Tool3 | 1 | 0

There should be NO duplicate toolName in output.

Thnaks,
Manu.
Go to the top of the page
 
+Quote Post
Eggie
post Jan 20 2008, 02:27 PM
Post #2


Advanced Member
Group Icon

Group: Members
Posts: 131
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735



i dont really understand your question....maybe if you posted it more clearly i would understand
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Jan 20 2008, 03:57 PM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 371
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



Use the following code to create the Table structure. It is better to create the Status field as bit which can have only two values/states 0 and 1.

CODE
CREATE TABLE mytable
(
IssueId INT AUTO_INCREMENT PRIMARY KEY,
ToolName VARCHAR(15) NOT NULL,
Status BIT
)


For inserting values use queries similar to:-

CODE
INSERT INTO mytable VALUES('', 'Tool1', 1);
INSERT INTO mytable VALUES('', 'Tool1', 0);
INSERT INTO mytable VALUES('', 'Tool2', 0);
INSERT INTO mytable VALUES('', 'Tool3', 1);


0 is equivalent to Closed and 1 to Open

To retrieve your result use the following query:-

CODE
SELECT DISTINCT ToolName, (SELECT COUNT(Status) FROM mytable t2 WHERE Status = '1' AND t1.ToolName = t2.ToolName), (SELECT COUNT(Status) FROM mytable t2 WHERE Status = '0' AND t1.ToolName = t2.ToolName)  FROM mytable t1


It contains two subqueries to do the job.

Given below are codes for implementing the Status as a normal VARCHAR but I would recommend using the BIT data type.

Table Structure

CODE
CREATE TABLE mytable
(
IssueId INT AUTO_INCREMENT PRIMARY KEY,
ToolName VARCHAR(15) NOT NULL,
Status VARCHAR(6)
)


Data Insertion

CODE
INSERT INTO mytable VALUES('', 'Tool1', 'Open');
INSERT INTO mytable VALUES('', 'Tool1', 'Closed');
INSERT INTO mytable VALUES('', 'Tool2', 'Closed');
INSERT INTO mytable VALUES('', 'Tool3', 'Open');


Data Output

CODE
SELECT DISTINCT ToolName, (SELECT COUNT(Status) FROM mytable t2 WHERE Status = 'Open' AND t1.ToolName = t2.ToolName), (SELECT COUNT(Status) FROM mytable t2 WHERE Status = 'Closed' AND t1.ToolName = t2.ToolName)  FROM mytable t1


This post has been edited by turbopowerdmaxsteel: Jan 20 2008, 03:58 PM
Go to the top of the page
 
+Quote Post
cybernaut
post Feb 15 2008, 09:02 AM
Post #4


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 14
Joined: 15-February 08
Member No.: 28,427



Try this query. I hope it is helpful.
QUOTE
SELECT issuedid, COUNT(status) FROM mytable GROUP BY status
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL - Trouble With Bulk Insert Statements(3)
  2. MySQL Realtime Replication(4)
  3. Recover Tables From A MySQL .frm File(8)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. How To Connect MySQL With Flash?(8)
  6. MySQL Output Database Question(18)
  7. MySQL, Multiple Tables(24)
  8. Navcat For MySQL(8)
  9. Permission Problem With Mysql Database Creation(8)
  10. Mysql And Php(15)
  11. Problems With Php Saving Data Into Mysql(6)
  12. Login System Using A Mysql Db(5)
  13. Oracle Vs. Mysql Vs. Postgresql(9)
  14. Subqueries In Mysql(1)
  15. Apache Php With Mysql On Windows [solved](9)
  1. Not Understanding Mysql(4)
  2. Mysql Script Help(3)
  3. Mysql - So Hard(14)
  4. Mysql Problem(1)
  5. Sun Bought Mysql(6)
  6. Mysql Backup With Another Address?(4)
  7. I Have An Error With My Mysql Connection(7)
  8. Mysql And User File_priv(0)
  9. Mysql Database Management(1)
  10. Mysql Database Entry By Excel Sheets(2)
  11. Mysql On Computer(9)
  12. Any Website Provide Free Host Mysql Host?(4)
  13. Mysql Multiple Tables(1)


 



- Lo-Fi Version Time is now: 5th September 2008 - 07:06 AM