1

I have a SQL command which I know data exists with the criteria, but when I run the query it doesn't return rows:

SQL:

SELECT `USER_ID`, `CAR_ID`, `AD_ID`, `BRAND`, `MODEL`
, `YEAR`, `PRICE`, `MILEAGE`, `GEARBOX`, `STATUS`, `COLOR_IN`, `COLOR_OUT`
, `BODY`, `FUEL`, `ABOUT`, `MAIN_PHOTO` 
FROM (`ads`, `cars`, `ad_extras`) 
WHERE `ads`.`USER_ID` = '2'

What should I do?

EDIT:

When I do SELECT * it also doesn't return data.

when I select from 2 tables it retaurns data. So I can't select from 3 tables right?

10
  • SELECT is not broken. It's far more likely to be your code. If you have no data returned by the SELECT statement then there is no data that matches your WHERE clause. If this is truely the case then please post sample data that we can run this SQL statement against to test it. Commented Dec 5, 2013 at 13:03
  • @Ben, I run this query on the db server without any other language. Commented Dec 5, 2013 at 13:04
  • I understand, however, you're assuming that the most basic functionality of the database has a catastrophic bug. Isn't it more likely that the data just doesn't exist? Although re-looking at your code do you mean that the query never finishes running? Commented Dec 5, 2013 at 13:05
  • 6
    What should you do? Start with, select count(*) from ads where user_id = 2. Commented Dec 5, 2013 at 13:06
  • How much data is there in the tables? You are joining three tables without any relations, so you would get a result where each record from cars is paired with every record from ad_extras. That might be a result that is too large for the server to complete. Commented Dec 5, 2013 at 13:13

2 Answers 2

2

It's likeky that one of your tables is empty. The result of that is empty.You should use a query like this one

SELECT `USER_ID`, `CAR_ID`, `AD_ID`, `BRAND`, `MODEL`, `YEAR`, `PRICE`, `MILEAGE`, `GEARBOX`, `STATUS`, `COLOR_IN`, `COLOR_OUT`, `BODY`, `FUEL`, `ABOUT`, `MAIN_PHOTO` FROM `ads` natural left join `cars` natural left join `ad_extras` WHERE `ads`.`USER_ID` = '2'

I wanted to post this like a comment but I don't have enought privilegies.

Sign up to request clarification or add additional context in comments.

Comments

0

In order to query from 3 table it is better to use JOIN clause between them

or if you want to write it as it is use alias for selecting columns

e.g

SELECT 'ads.USER_ID', 'cars.CAR_ID', 'ads.AD_ID'
FROM ('ads', 'cars', 'ad_extras') 
WHERE 'ads'.'USER_ID' = '2'

with their respective table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.