Sachintha Senanayake
Sachintha Senanayake

Reputation: 29

Select specific rows and columns from an MySQL database

I'm currently doing a student attendance program using c# and MySQL.

I created a database called std_info where it keeps students information and there are nibm_id, nic, name,address,number,batch in that table

2nd table name is std_att to get student attendance and the columns are nibm_id, nic, name, batch

What i want is to retrive only nibm_id, nic, name, batch from std_info table to std_att The primary key is nibm_id

here is what i was trying to do so far

cmd.CommandText = "insert into std_att (nibm_id, nic, name, batch) SELECT * (nibm_id, nic, name, batch)  FROM `std_info` where nibm_id like '" + textBox1.Text + "%'";

Please show me a way to do this. Thank You!

Upvotes: 0

Views: 236

Answers (1)

Stuart
Stuart

Reputation: 32

cmd.CommandText = "insert into std_att (nibm_id, nic, name, batch) SELECT nibm_id, nic, name, batch  FROM `std_info` where nibm_id = '" + textBox1.Text + "%'";

There are obviously a lot better ways to get this done, and the above is vulnerable to SQL injections etc, but I can understand if this is for a simple quick piece of work.

Like some of the users have said, don't have duplicate columns in std_att table since they are already in the main table std_info

I would also try to find the nibm_id before the insert bit, to validate in-app and not let the db freak out on it's side (if nibm_id can't be found)

Upvotes: 2

Related Questions