You've not shown us the schema of your table; that makes it harder than it might be to help you.
If you are seeking to get car1
column or car2
column or ... then there are at least two problems:
- Your table is incorrectly designed.
- You cannot build up the name of a column (or table) as part of the query.
You should put the information about each of a customer's cars in a separate table from the information about the customer. Thus, the information that the customer's name is 'Joe' belongs in one table; the fact that he drives a Lamborghini should be recorded in a separate table. There might be multiple records in the table of cars for each customer.
You'd also need to join the two tables; your syntactically incorrect query doesn't attempt to do that.
You have two tables with columns:
Customers: customer, type, ...
Customers_Cars: main_vehicle, car1, car2, car3
You probably have, but haven't shown, some sort of 'customer' column in Customers_Cars
. In fact, you should probably have a 'Customer ID' column in each table. Further, the Customers_Cars
table should be just:
- Customers_Cars: CustomerID, CarNum, CarInfo, MainVehicle
In this, I'm assuming MainVehicle
is a boolean flag, and that there is one entry tagged true per customer.
Your SELECT statement then becomes:
SELECT U.*,
C.CarInfo
FROM Customers AS U
JOIN Customers_Cars AS C ON C.CustomerID = U.CustomerID
WHERE C.MainVehicle = TRUE
AND U.Customer = 'Joe'
AND U.Type = 'CoolCustomer';