Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Photo
- - - - -

Mysql Query Question


5 replies to this topic

#1 Manu Dhanda

Manu Dhanda

    Newbie [ Level 1 ]

  • Members
  • 1 posts

Posted 20 January 2008 - 01:51 PM

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.

#2 Eggie

Eggie

    Premium Member

  • [HOSTED]
  • 263 posts
  • Gender:Male
  • Location:Zagreb/Croatia
  • myCENTs:2.27

Posted 20 January 2008 - 02:27 PM

i dont really understand your question....maybe if you posted it more clearly i would understand

#3 turbopowerdmaxsteel

turbopowerdmaxsteel

    Premium Member

  • [HOSTED]
  • 449 posts
  • Gender:Male
  • Location:Kolkata, India
  • Interests:Programming, Gaming, Cricket, Listening to Music and watching action cartoons.
  • myCENTs:30.99

Posted 20 January 2008 - 03:57 PM

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.

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

For inserting values use queries similar to:-

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:-

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

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

Data Insertion

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

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

Edited by turbopowerdmaxsteel, 20 January 2008 - 03:58 PM.


#4 cybernaut

cybernaut

    Newbie [ Level 2 ]

  • Members
  • 14 posts

Posted 15 February 2008 - 09:02 AM

Try this query. I hope it is helpful.

SELECT issuedid, COUNT(status) FROM mytable GROUP BY status



#5 Guest_Sara_*

Guest_Sara_*
  • Guests

Posted 09 April 2012 - 06:32 AM

SELECT `ToolName`, SUM(Status=1), SUM(Status=0) FROM `tools` GROUP BY `ToolName` ORDER BY `ToolName` ASC
I find this as easier than using sub queries

#6 kunbobo

kunbobo

    Newbie [ Level 1 ]

  • Members
  • 1 posts

Posted 13 June 2012 - 08:53 AM

in your .php
<?php
$con = mysql_connect("hostname","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
//Create your database
mysql_query("CREATE DATABASE database-name ",$con);
//Create your table
mysql_select_db("database-name", $con);
$sql = "CREATE TABLE my_table

(
IssueId INT AUTO_INCREMENT PRIMARY KEY, ToolName VARCHAR(15) NOT NULL, Status VARCHAR(6)
)";
//insert
mysql_query("INSERT INTO my-table (IssueId, ToolName, Status) VALUES ('...', '...',...)")
VALUES ('...', '...',...) ... VALUES('...','...','...')");

Edited by yordan, 13 June 2012 - 01:11 PM.
added the code tag




Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users