Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have this piece of code, where it shows my sql query string, but how can i setup the query to show the data from 2 tables?(entradas,saidas)


C#
string SQL = "SELECT FROM entradas,saidas(entradas.(id_veiculo,id_empresa,nome_condutor,empresa_visitante,empresa_visitar,visitado,ncartao,data,hora,obs,sector), saidas.(DATA,HORA,OBS)) WHERE id_empresa = @tabempresa; ";

using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
{
    using (var cmd = new MySqlCommand(SQL, cn))
    {
        cmd.Parameters.AddWithValue("@tabempresa", tabControl1.SelectedIndex);
        MySqlDataAdapter sda = new MySqlDataAdapter();
        DataTable dt = new DataTable();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        cn.Open();

        try
        {
            DataView dv = new DataView(dt);
            Grid1.DataSource = dv.Table;
            Grid1.AutoGenerateColumns = true;
            cmd.ExecuteNonQuery();
        }
        catch (MySqlException ex)
        {
            // Do some logging or something.
            MessageBox.Show("There was an error accessing your data. DETAIL: " + ex.ToString());
        }
        cn.Close();
    }
}


What I have tried:

tried to use normal select syntax to show up both tables on one datagrid.
Posted
Updated 1-Mar-16 1:10am

1 solution

Change query to:
SQL
SELECT * FROM entradas a LEFT OUTER JOIN saidas b
ON a.data = b.data AND a.hora=b.hora and a.sector = b.sector
WHERE id_empresa = @tabempresa;


NAMED:

SQL
SELECT a.id_veiculo, a.id_empresa, a.nome_condutor, a.empresa_visitante, a.empresa_visitar, a.visitado, a.ncartao, a.data, a.hora, a.obs,a.sector, b.data as Data1, b.hora as hora1 FROM entradas a LEFT OUTER JOIN saidas b
ON a.data = b.data AND a.hora=b.hora and a.sector = b.sector
WHERE id_empresa = @tabempresa;
 
Share this answer
 
v3
Comments
Scribling Doodle 1-Mar-16 7:12am    
Do i need to use "inner join" or "full join" to work? I'm not much into sql join structure...
Herman<T>.Instance 1-Mar-16 7:14am    
what is in entradas? What is the Key that both tables connect?
I did change the query based on the column names you gave.
Scribling Doodle 1-Mar-16 7:15am    
The sql query doesn't work, what could be the problem here? I want to filter data from 2 tables, one that has the car join values and the exit values, in "DATA and HORA" i want it to be joined like car joined at 13.3.2016 / 22:15 and left at 14.3.2016 / 22:15
Herman<T>.Instance 1-Mar-16 7:16am    
query is changed now
Scribling Doodle 1-Mar-16 7:21am    
https://gyazo.com/fce36c6b8c311d33e70ba1721b84bfe8 Here you can see all the tables i have, the 2 that have the columns are the ones i want to use to show data in my datagrid. Hope it helps :)

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