N
N
Nikita Verdinsky2020-05-24 19:01:21
MySQL
Nikita Verdinsky, 2020-05-24 19:01:21

How to separate the data received by the query into different forms?

I have 3 tables.

questions answers
5eca93f409539218925569.png

subjects By this query
5eca94039afc2168282715.png


5eca9412d6023353530384.png

SELECT questions.question_text, answers.answer_1, answers.answer_2, answers.answer_3, answers.answer_4 FROM questions  JOIN answers ON answers.ID = questions.ID_answer
I post questions and answers.
Question: how can I separate these questions and answers depending on the subject_title? And if the subject_title has the same value, then the received data should be combined

. Now I'll show it clearly.
Here I choose one of the options.
5eca96d56d175002148286.png
The following form opens for me, which shows all the data that I received by the request, and I need the data to match the selected option (subject_title and subject_name), that is, if I select "Russian language" "Punctuation", then two questions should be displayed and answers about "dot" and "comma".
5eca96fc371a4205370743.png

Here are all the codes you may need to understand the whole situation.
ListItemTest
namespace WindowsFormsApp1
{
    public partial class ListItemTest : UserControl
    {
        public ListItemTest()
        {
            InitializeComponent();
        }

        #region Proporties
        private string _question;
        private string _answer1;
        private string _answer2;
        private string _answer3;
        private string _answer4;


        [Category("Custom Props")]
        public string Question
        {
            get { return _question; }
            set { _question = value; lbl_question.Text = value; }
        }

        [Category("Custom Props")]
        public string Answer1
        {
            get { return _answer1; }
            set { _answer1 = value; rb_answer1.Text = value; }
        }

        [Category("Custom Props")]
        public string Answer2
        {
            get { return _answer2; }
            set { _answer2 = value; rb_answer2.Text = value; }
        }

        [Category("Custom Props")]
        public string Answer3
        {
            get { return _answer3; }
            set { _answer3 = value; rb_answer3.Text = value; }
        }

        [Category("Custom Props")]
        public string Answer4
        {
            get { return _answer4; }
            set { _answer4 = value; rb_answer4.Text = value; }
        }
        #endregion

    }
}

5eca98ec94b28388708455.png

private void LastNumber()
        {

            string connStr = "server=localhost; port=3306; username=root; password= root; database=vedar_bd";
            string sql = "SELECT COUNT(DISTINCT question_text) FROM questions";
           // string sql = "SELECT * FROM Table WHERE Field IN (SELECT Field FROM Table GROUP BY Field HAVING COUNT(*) > 1)";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            MySqlCommand command = new MySqlCommand(sql, conn);

            string number = command.ExecuteScalar().ToString();

            int xnumber = int.Parse(number);
            SubjNumber = xnumber;
        }

        private void OneTest()
        {
            string connStr = "server=localhost; port=3306; username=root; password= root; database=vedar_bd";
            string sql = "SELECT questions.question_text, answers.answer_1, answers.answer_2, answers.answer_3, answers.answer_4 " +
                "FROM questions  JOIN answers ON answers.ID = questions.ID_answer"; 

            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            MySqlCommand command = new MySqlCommand(sql, conn);

            MySqlDataReader SomeTest;

            DataTable tableTest = new DataTable();

            SomeTest = command.ExecuteReader();
            tableTest.Load(SomeTest);

            ListItemTest[] listitems = new ListItemTest[SubjNumber];
            for (int i = 0; i < SubjNumber; i++)
            {
                listitems[i] = new ListItemTest();
                listitems[i].Question = tableTest.Rows[i][0].ToString();
                listitems[i].Answer1 = tableTest.Rows[i][1].ToString();
                listitems[i].Answer2 = tableTest.Rows[i][2].ToString();
                listitems[i].Answer3 = tableTest.Rows[i][3].ToString();
                listitems[i].Answer4 = tableTest.Rows[i][4].ToString();           

                if (flowLayoutPanel1.Controls.Count < 0)
                {
                    flowLayoutPanel1.Controls.Clear();
                }
                else
                    flowLayoutPanel1.Controls.Add(listitems[i]);
            }
            conn.Close();
        }

        private void Frm_TestView_Load(object sender, EventArgs e)
        {
            LastNumber();
            OneTest();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-05-24
@Nie_yar

How to separate the data received by the query into different forms?
but you don’t need to divide,
you need to make adequate queries for each
ps form, but for optimization you can use temporary tables or views,
but this is when you understand well
pps
вы можете сделать какой то запрос к БД, и получить набор данных в памяти. Array/List/IEnumerable/Dictionary
и делить уже этот набор. upd допустим средствами LINQ
хотя это просто альтернатива временным таблицам или view. в любом случае вам все таки надо понять что и в какой момент времени происходит. а для этого надо двигаться от простых схем данных/логики/предсталения, к более сложным, чуть помедленнее

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question