Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi everbody,

I am new in programming. My problem is that I do not know how the select count command correctly works.
My table looks like that:
ID (primary key), agent_ID (foreign key), time (it is only a string), totalamount (dec)

every agent (medicine) can have only one totalamount per time (quarter). So if a user wants to store a new dataset this must be proofen.

select count (*) from table where ????

thanks
Posted
Comments
virusstorm 19-Jun-15 12:17pm    
First, your "time" column should be a "DATEIME" type, not a string.

Second, can you provide some sample data of your table will have and what output you need? I'm not sure I follow what you are trying to count.
Member 11778317 19-Jun-15 13:59pm    
First I tried it with datetime but it didn't worked for me.

I have one table with basic information. It is ID, activeagent (stands for antibiotics), DDD (daily defiened dose), and ATC-Code (from the WHO). The second table is used to record the consumption for each antibiotic within on quarter (for example 01/15 january to march). For example 200 g (=totalamount) Penicilline. Later I want to devide the totalamount / DDD. Each antibiotic can have only one totalamount per quarter.

Hope it helps.

thanks
CHill60 19-Jun-15 17:52pm    
Member asked for "sample data" ... that means some sample data would be useful.
Not entirely convinced that datetime would not work for you... given that it works for everybody (and I mean that) else then you need to explain why it didn't work "for you" - you've said that you are a beginner, so take it from the experienced guys - use DateTime NOT string.
Try to make your requirements a little clearer so that we can help
j snooze 19-Jun-15 17:53pm    
You didn't mention a second table in your first post. Sounds like this second table might be important. If you spend a little time like virusstorm suggests and give us a table layout sample of each table with some sample data...and then what you need it to look like, we can probably answer very quickly. It will require you to type less to explain what you need by showing us. thanks!
Member 11778317 20-Jun-15 5:10am    
Thanks a lot so far.

Here is a sample of the two tables (I hope this is OK)

<pre lang="text">Table Wirkstoffe;

ID: 11 (autoincrement)
ATCCode: J01DC02 (string)
activeagent: Cefuroxim (string)
form: i.v. (string)
DDD: 3.00 (decimal)

ID: 9
ATCCode: J01CA04
activeagent: Amoxicillin
form: i.v.
DDD: 2

ID: 9
ATCCode: J01CA04
activeagent: Amoxicillin
form: p.o.
DDD: 2

table: calculatedconsumption
ID: 23
agent_ID: 11 (foreign key)
totalamount: 100 (decimal)
time: 01/15 (until now string but will changed to datetime) (user can select 01/15, 02/15,0315,04/15 from a combobox)
ward_ID: foreign key (table ward with ward ICU, etc)
dailydose: totalamount / DDD
consumptiondensity: (dailydose / count of patient days) * 100 (count of patient days is not set yet)
</pre>

User can insert for each activeagent per time per ward one totalamount using a windows form. for now is it not necessary to distinguish between the form (i.v. or p.o.). Problem now how can I controll if a record e.g. Cefuroxim 01/15 ICU already exists?

here is a sample for insert data into calculatedconsumption (ward is not set yet)
<pre>private void button5_Click(object sender, EventArgs e)
{
string mycon = "server=xxx.xxx.xxx.xx;port=3306;username=xxx;password=xxx;database=Antibiotikaverbrauch;";
string dbselectID = "select ID from Wirkstoffe where activeagent='" + comboBox4.Text + "'; ";
MySqlConnection myconnection = new MySqlConnection(mycon);
MySqlCommand cmd = new MySqlCommand(dbselectID, myconnection);
MySqlDataReader myreader;

try
{
myconnection.Open();
myreader = cmd.ExecuteReader();
while (myreader.Read())
{
string sid = myreader.GetString("ID");
int snid = Convert.ToInt32(sid);
MessageBox.Show(sid, dbselectID);
string mycon2 = "server=xxx.xxx.xxx.xx;port=3306;username=xxx;password=xxx;database=Antibiotikaverbrauch;";
string dbinsert = "insert into calculatedconsumption (time,totalamount,agent_ID) values ('" + this.comboBox5.Text + "','" + this.textBox6.Text + "','" + snid + "'); ";
MySqlConnection myconnection2 = new MySqlConnection(mycon2);
MySqlCommand cmd2 = new MySqlCommand(dbinsert, myconnection2);
MySqlDataReader myreader2;
myconnection2.Open();
myreader2 = cmd2.ExecuteReader();
myconnection2.Close();
myreader2.Close();
}
myreader.Close();
myconnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}</pre>

I now it is not good but I am realy a beginner and I learn every day something new :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900