Malakai
Malakai

Reputation: 3131

inner join 4 tables mysql C#

I really need a help. To begin with i'd like to present what's the problem :

General Table:
create table cworkdb.models(
    model_id integer auto_increment,
    model_name varchar(40) not null,
    man_id integer not null,
    cpu_id integer not null,
    ram_quantity integer not null,
    hdd_capacity integer not null,
    video_id integer not null,
    primary key(model_id),
    foreign key(man_id) references manufacturers(man_id),
    foreign key(cpu_id) references cpu_comp(cpu_id),
    foreign key(video_id) references videocard(video_id)
);

And 3 tables(which I have link to):

create table cworkdb.manufacturers(
    man_id integer auto_increment,
    man_name varchar(50) not null,
    primary key(man_id)
);
create table cworkdb.videocard(
    video_id integer auto_increment,
    video_name varchar(45) not null,
    video_memory double not null,
    primary key(video_id)
)
create table cworkdb.cpu_comp(
    cpu_id integer auto_increment,
    cpu_name varchar(60) not null,
    cpu_freq double not null,
    primary key(cpu_id)
);

My selectQuery string -

string selectQuery = "select model_id, model_name, man_name, cpu_name,ram_quantity,hdd_capacity,video_name"
                                 + "from (cworkdb.models inner join cworkdb.cpu_comp on cworkdb.models.cpu_id=cworkdb.cpu_comp.cpu_id)"
                                 +"inner join cworkdb.manufacturers on cworkdb.models.man_id=cworkdb.manufacturers.man_id"
                                 +"inner join cworkdb.videocard on cworkdb.models.video_id=cworkdb.videocard.video_id";

When I'm trying to use this in MySqlCommand - an error is thrown:

the problem is near first inner join.

Although I modified (deleted first brackets), it still says there's an error. Where did I make the mistake? Any Help will be appreciated.

Upvotes: 1

Views: 986

Answers (2)

Joe Taras
Joe Taras

Reputation: 15379

You have write this:

string selectQuery = "select model_id, model_name, man_name, cpu_name,ram_quantity,hdd_capacity,video_name"
                             + "from (cworkdb.models inner join cworkdb.cpu_comp on cworkdb.models.cpu_id=cworkdb.cpu_comp.cpu_id)"
                             +"inner join cworkdb.manufacturers on cworkdb.models.man_id=cworkdb.manufacturers.man_id"
                             +"inner join cworkdb.videocard on cworkdb.models.video_id=cworkdb.videocard.video_id";

but you're missing the space at the end of row (or if you want at the start of row), so when you concatenate the strings inner join is attached to )

So write this:

string selectQuery = "select model_id, model_name, man_name, cpu_name,ram_quantity,hdd_capacity,video_name "
                             + "from (cworkdb.models inner join cworkdb.cpu_comp on cworkdb.models.cpu_id=cworkdb.cpu_comp.cpu_id) "
                             +"inner join cworkdb.manufacturers on cworkdb.models.man_id=cworkdb.manufacturers.man_id "
                             +"inner join cworkdb.videocard on cworkdb.models.video_id=cworkdb.videocard.video_id";

Upvotes: 2

Nullius
Nullius

Reputation: 2702

You should add spaces to your SQL sentences.

string selectQuery = "select model_id, model_name, man_name, cpu_name,ram_quantity,hdd_capacity,video_name "
                             + "from (cworkdb.models inner join cworkdb.cpu_comp on cworkdb.models.cpu_id=cworkdb.cpu_comp.cpu_id) "
                             +"inner join cworkdb.manufacturers on cworkdb.models.man_id=cworkdb.manufacturers.man_id "
                             +"inner join cworkdb.videocard on cworkdb.models.video_id=cworkdb.videocard.video_id";

Upvotes: 1

Related Questions