select uit 3 tabellen

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
Hallo allemaal,

IK heb drie tabellen: kast, plank, boek.

nu is de bedoelingdat je ziet: welk boek op welke plank staat en de plank in welke kast

Heb dit:

Code:
 myCommand.CommandText = ("SELECT kast.kast_id,plank.plank_id,boek.boek_id" +
            "FROM kast INNER JOIN plank ON plank.plank_id = kast.kast_id " +
            "INNER JOIN boek ON boek.boek_id = kast.kast_id AND boek.boek_id = plank.plank_id ");

Alvast bedankt voor de hulp.

Niels.
 
[SQL]
SELECT kast_id, plank_id, boek_id FROM
boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id
[/SQL]
 
Hoi Leon d,

enorm bedankt voor je snelle reactie.

Maar krijg nog wel deze error:

Code:
Syntax error (missing operator) in query expression 'boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id'.

Niels.
 
hoe zie je code er nu uit? Het lijkt erop dat de query niet goed in de variabele gaat
 
Hoi Leon d,

het is in C#:

dit is de volledige code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
//using System.Data.OleDB; 

namespace CustomDataReaders
{
    class Program
    {
        Random RAND = new Random();
        static void Main(string[] args)       
        {

           
            //Create a connection
    OleDbConnection m_cnADONewConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\meetSysteem.mdb");
    //open the connection
    m_cnADONewConnection.Open();
    //Create a command object.
    OleDbCommand myCommand = m_cnADONewConnection.CreateCommand();
    //Specify a query
    myCommand.CommandText = ("SELECT kast_id, plank_id, boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id");
   
            
    OleDbDataReader myDataReader = myCommand.ExecuteReader();


    //Display the resulting Data
    while (myDataReader.Read())
   {

       Console.WriteLine(" kast nr:{0}   ", myDataReader["kast_nr"]);
       Console.WriteLine(" plank nr:{0}   ", myDataReader["plank_nr"]);
       Console.WriteLine(" boek nr:{0}   ", myDataReader["boek_nr"]); 
   }

 
    //Close the DataReader.
    myDataReader.Close();
    //Close the connection
    m_cnADONewConnection.Close();

 
    //Keep the console open until a keystroke.
    Console.ReadKey(true);

           



        }

       
    }
}
 
Hoi Leon,

wellicht handig als ik je ook de acces database opstuur?

In dit geval.Wat is je email?

Niels.
 
Nou ik ben niet bekend met C# ik weet wel dat de query gewoon goed is.
Je gebruikt MSSQL misschien moet je alleen JOIN gebruiken ipv INNER JOIN
maar de query is verder in orde MITS de velden ook allemaal bestaan!
 
Trouwens,

Voeg eens een ; aan het einde van de query als volgt:
ON plank.kast_id = kast.kast_id;");


Werkt dit niet, dan moet je de query even in Access zelf uitproberen. Ik heb hier geen Access erop staan dus kan ook de dbase niet bekijken.
 
Laatst bewerkt:
hoi Leon,

ik heb de INNER weg gelaten, maar dan krijg ik de volgende error:

Syntax error in FROM clause.

de velden kloppen allemaal.

Niels.
 
Hoi Leon,

wat bedoel je?

heb het zo:

Code:
("SELECT kast_id, plank_id, boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER  JOIN kast ON plank.kast_id = kast.kast_id");

maar dan de volgende error:
Syntax error (missing operator) in query expression 'boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id'.
 
dan achter kast.kast_id een ; plaatsen

en als dat niet werkt de query even in access plakken
 
Code:
 myCommand.CommandText = ("SELECT kast_id, plank_id, boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER  JOIN kast ON plank.kast_id = kast.kast_id;");

error:

Syntax error (missing operator) in query expression 'boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id'.
 
Bij de tweede INNER JOIN zitten er twee spaties tussen INNER en JOIN misschien dat dat iets uit maakt?
 
Code:
 myCommand.CommandText = ("SELECT kast_id, plank_id, boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER  JOIN kast ON plank.kast_id = kast.kast_id;");

error:

Syntax error (missing operator) in query expression 'boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id'.

Ik denk dat ik het al weet.
gebruik de volgende query:
[sql]
SELECT kast.kast_id, plank.plank_id, boek.boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id
[/sql]

Hij wist niet welke kast_id hij moest selecteren, aangezien deze in meerdere tabellen voorkomt. Deze heb ik nu zelf getest (heb ff snel data aangemaakt) en werkt in MYSQL iig
 
Hoi Leon,

ja, het is mssql.

Code:
"SELECT  kast_id , plank_id ,  boek_id  FROM boek INNER JOIN plank ON  boek.plank_id = plank.plank_id INNER  JOIN kast ON plank.kast_id = kast.kast_id";

Werkt niet. Althans zegt dat er een invalid syntax is. Maar hoe je moet verbeteren?

Niels.
 
[sql]SELECT kast.kast_id, plank.plank_id, boek.boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id[/sql]

probeer dat eens. En haal die dubbele spatie weg tussen de tweede INNER en JOIN
 
Heb nu dit:

Code:
"SELECT kast.kast_id, plank.plank_id, boek.boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id";


Code:
Syntax error (missing operator) in query expression 'boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id'.
 
Laatst bewerkt:
Hoi,

ja, krijg dan de volgende error:

invalid SQL statement; expexted 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'

Code:
"SELECT kast.kast_id, plank.plank_id, boek.boek_id FROM boek INNER JOIN plank ON boek.plank_id = plank.plank_id INNER JOIN kast ON plank.kast_id = kast.kast_id"
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan