Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I have a table that is supposed to store car information regarding license plate, model, brand and many other things. The problem is that I have another table called brand and in it has all the possible car brands that actually exists. On the table, it inserts the ID from the brand instead of the name. But when I want to show on the combobox ("License_plate - Brand - Model - Year_month") the brand is setting up to the ID instead of the name.

For example:

If i select the Toyota brand, the ID for that brand is 1. If so, the table will look something like this:

Data values inserted on `Cars` table
IDCAR: "1"
Brand: "1" -> Referred to TOYOTA on the table `Brands`
License_plate: "22-33-AD"
Model: "Yaris"
Year_month: "17/05"


Tables:

km_carro (referred to `Cars` in this topic)
CREATE TABLE `km_carro` (
  `IDCARRO` int(11) NOT NULL AUTO_INCREMENT,
  `MATRICULA` varchar(50) NOT NULL,
  `ANO_MES` varchar(50) NOT NULL,
  `MODELO` varchar(50) NOT NULL,
  `MARCA` int(11) NOT NULL,
  `OBS` text NOT NULL,
  `COLABORADOR` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`IDCARRO`),
  KEY `FK_MARCA` (`MARCA`),
  KEY `FK_MODELO` (`MODELO`),
  KEY `FK_COLABORADOR` (`COLABORADOR`),
  CONSTRAINT `FK_MARCA` FOREIGN KEY (`MARCA`) REFERENCES `km_marca` (`IDMARCA`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8


km_marca ( referred as `Brands`)
CREATE TABLE `km_marca` (
  `IDMARCA` int(11) NOT NULL AUTO_INCREMENT,
  `NOME` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`IDMARCA`)
) ENGINE=InnoDB AUTO_INCREMENT=69 DEFAULT CHARSET=utf8

What I want:

Simply show the car data on a combobox regarding another form.

What I have tried:

Actually, I have written this code based on another project that is using ID-Name on the combobox and I've managed to sort it like I want, but instead of the Brand_name, it gives me the 'Brand_ID'
VB
da.SelectCommand = New MySqlCommand("SELECT IDCAR, Concat(License_plate ,' - ', Brand, ' - ', Model, '-', Year_month) As 'CAR' from zestagio.cars ", con)
cmbCarro.DataSource = dt
       cmbCarro.DisplayMember = "Carro"
       cmbCarro.ValueMember = "IDCARRO"
Posted
Updated 23-May-17 0:37am
v4

You have to JOIN to the brand table e.g.
.SelectCommand = New MySqlCommand("SELECT IDCAR, Concat(License_plate ,' - ', BrandName, ' - ', Model, '-', Year_month) As 'CAR' from zestagio.cars AS C LEFT JOIN zestagio.brand AS B ON C.Brand=B.Brand;", con)
I've assumed that the foreign key on the car table is [brand] and links to the [brand] column on the brand table, and that the brand name is in a column called [brandname]. Change those details to match your tables

[UPDATE] - Here is a useful CodeProject article on joins Visual Representation of SQL Joins[^]
 
Share this answer
 
v5
Comments
Scribling Doodle 23-May-17 6:11am    
Yeah, that's right, I'll try it out. The problem is that it's showing the value ID for the brand, instead of the real name. But i'll try it and see if that helped ;)
EDIT: Still showing the Brand ID, instead of the brand name. But the Sql syntax is working fine. It's not simply changing that '1' to Toyota.
CHill60 23-May-17 6:13am    
You have to use the column from the brand table that contains the name not the id
Scribling Doodle 23-May-17 6:18am    
Yeah right, That's what i've done, i simply have the brand name called Name. I even changed the last part to c.brand = b.Name
CHill60 23-May-17 6:26am    
"I even changed the last part to c.brand = b.Name" - what?
Post the sample record from the Brand table with the actual column names
Scribling Doodle 23-May-17 6:29am    
Maybe you will need to translate since my table is entirely in Portuguese...

I'll update the question with the tables.
EDIT: Saw that article, and figured out what's wrong. Already posted an answer regarding your answer.
Regarding @CHiLL60 and his answer, the problem was defining the Left join tables. If so, this syntax should do the job.

"SELECT IDCAR, Concat(License_plate ,' - ', Name, ' - ', Model, '-', Year_Month) As 'CAR' from zestagio.car AS C LEFT JOIN zestagio.brand AS B ON C.brand=B.idbrand;"
 
Share this answer
 
Comments
CHill60 23-May-17 6:49am    
I'm guessing "Name" should actually be 'Nome'
Scribling Doodle 24-May-17 9:30am    
Yeah, you're right mate, thanks in advance!

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