Hello I have got the problem with my query and it display the correct data but not correct amount (count (*)). Somehow it should display the data as 1 for that part but it display 4.
I don’t know where is the error is.
select Vehical_Type,
Vehical_Registration_No,
count(*)
from van,
van_booking
where Vehical_Type = 'Ford - Transit'
and Vehical_Registration_No <> fk2_Vehical_Registration_No
not in (select fk2_Vehical_Registration_No
from van_booking
where '2010.10.12' between Hire_Start_Date and Hire_End_Date
or '2010.10.11' between Hire_Start_Date and Hire_End_Date);
Please can anyone help me?
Where do we start?
Since there is no “AND ” before the NOT IN, so
At least try to post a working query since you seem to indicate it works (and returns count=>4).
Secondly, please alias the table names so that you can prefix the columns with the alias names. At present, it is ambiguous as to which columns belong to which tables.
Thirdly, I guess this is MySQL? You should always state which DBMS you are working with, but MySQL is the only one that will let you get away with MIXING aggregate and non-aggregate fields WITHOUT a GROUP BY clause.
Fourthly, please try to learn to use SQL92 ANSI joins instead of the old-style cross-join and filter-in-WHERE. It looks like you have crossed Van and Van_Booking tables without any correlation clause. What are you trying to achieve?
Lastly, and this ties to the last point, it would greatly help to list out some sample data rows and what the result should look like based on that sample data.
I am going to guess that what you are actually after is a list of vehicle registration numbers of the type requested, but at the same time, in the COUNT column, show… the total number of records matching the query? So, something like this?
It’s just a guess, but without the count, you can use this
To include the count (which would be the same value in all rows), you can duplicate the query as a subquery to itself