Keeps saying error 105 - mysql

I'm trying to write q query for my sql sever for a database where i retrieve the HotelID's and names of all Hotels in Melbourne that have King size beds but it comes up with HotelID in field list is ambiguous i dont know how to solve this? this is my query
SELECT City, BedTypeDesc, HotelName, HotelID
FROM BedTypes, Hotels, Cities, Rooms
WHERE Hotels.CityID = Cities.CityID AND Hotels.HotelID = Rooms.HotelID AND Rooms.BedTypeID = BedTypes.BedTypeID AND BedTypeDesc = 'King Size' AND City = 'Melbourne'

You should use table aliases an proper join syntax:
SELECT c.City, bt.BedTypeDesc, h.HotelName, h.HotelID
FROM Hotels h JOIN
Cities c
ON h.CityID = c.CityID JOIN
Rooms r
ON h.HotelID = r.HotelID JOIN
BedTypes bt
ON r.BedTypeID = bt.BedTypeID
WHERE bt.TypeDesc = 'King Size' AND c.City = 'Melbourne' ;

Related

I want to Query w3school database

The database is at: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all . Particulary I want to get the list of customers who placed product orders on July 1996 from Speedy Express Shipping Company and were served by employees: Davolio Nancy and Peacock Margaret. The result should have:
Name of customer, contact, address, city, postal code and country
The orderDetailID they placed
Contacts of the shipping company
Product name, price and quantity
Product name, price and units bought
Category of the products
Here is my query:
SELECT
c.CustomerName,
c.ContactName,
c.Address,
c.City,
c.PostalCode,
c.Country,
o.OrderDetailID,
s.Phone,
r.ProductName,
r.Price,
o.Quantity,
g.CategoryName
FROM Customers c
JOIN Orders d
on (c.CustomerID = d.CustomerID)
LEFT JOIN OrderDetails o
on (o.OrderID = d.OrderID)
LEFT JOIN Shippers s
on (s.ShipperID = d.ShipperID)
LEFT JOIN Products r
on (r.ProductID = o.ProductID)
LEFT JOIN Categories g
on (g.CategoryID = r.CategoryID)
LEFT JOIN Employees e
on (e.EmployeeID = d.EmployeeID)
WHERE s.ShipperName = 'Speedy Express' AND
((e.LastName = 'Davolio' AND e.FirstName = 'Nancy') OR
(e.lastName = 'Peacock' AND e.firstName = 'Margaret')) AND
EXTRACT(YEAR_MONTH FROM d.OrderDate) = 199607;
I get error which says:
ERROR 1: Could not prepare statement (1 near "FROM": syntax error)
When I remove the last condition (of extracting the date) as follows:
SELECT c.CustomerName, c.ContactName, c.Address, c.City, c.PostalCode, c.Country, o.OrderDetailID, s.Phone, r.ProductName, r.Price, o.Quantity, g.CategoryName FROM Customers c JOIN Orders d on (c.CustomerID = d.CustomerID) LEFT JOIN OrderDetails o on (o.OrderID = d.OrderID) LEFT JOIN Shippers s on (s.ShipperID = d.ShipperID) LEFT JOIN Products r on (r.ProductID = o.ProductID) LEFT JOIN Categories g on (g.CategoryID = r.CategoryID) LEFT JOIN Employees e on (e.EmployeeID = d.EmployeeID) WHERE s.ShipperName = 'Speedy Express' AND((e.LastName = 'Davolio' AND e.FirstName = 'Nancy') OR (e.lastName = 'Peacock' AND e.firstName = 'Margaret'));
the error disappears. So please how can i fix the year_month condition without getting error!
One possibility here is that your version of MySQL, for whatever reason, does not support YEAR_MONTH being used with EXTRACT(). One workaround here would be to use DATE_FORMAT() instead:
WHERE DATE_FORMAT(d.OrderDate, '%Y%m') = '199607'
To test whether you have an old version of EXTRACT(), just try running the following simple query:
SELECT EXTRACT(YEAR_MONTH FROM NOW());
If this errors out, then my conjecture is correct.

Show names from two values

I am trying to get a SQL code but can't really figure out how to do it so I will explain what I want.
I have 4 tables called Person, Customer, Adres and Store. Now I have to show each customer NAMES which lives in the same city as where there is a Store. So First I figured out which persons are customers by:
SELECT person_name
FROM person
WHERE person_id IN
(SELECT Person_Person_Id
FROM customer);
Which stores are in which city:
SELECT Store_name, adres_city
FROM store s, adres a
WHERE s.Adres_Adres_Id = a.adres_id;
Note that person_person_id is the same as person_id just as a fk.
I am stuck at this code and don''t know how to go further from here. My column name of table adres = adres_city.
Okay, if I realised what do you want, try to do this:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
If you are not sure, that all your keys in tables are uniq, uncomment --distinct in the first string.
Or, if you are want to know statistics among your cities, do this:
select
b.Adres_City,
count(distinct a.person_id) as cnt
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
group by b.Adres_City
Please let me know, if it will help you.
Update1:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
where
b.Adres_City in (
select y.Adres_City
from dbo.Store x join dbo.Adres y on y.Adres_Id = x.Adres_Adres_Id
)

SQL different condition multi select

What I want to do (for each flight), is to select Flight_number, Departure_airport' s Name And Arrival_airport' s Name . Departure has MIN Leg_number, Arrival has MAX Leg_number.
I have tried this. But join parts or what else missing, here is the link:http://sqlfiddle.com/#!2/263a2/5
Seems odd.. but this might be what you're after...
We get the min/max leg for each flight in subquery aliased "Z"
We use this to join back to flight_leg twice, once for departure and once for arrivals
and again join back twice to airport once for departures once for arrivals.
SELECT Z.Flight_Number, DA.Name DeptName, AA.Name ArrivName
FROM (SELECT MIN(Leg_Number) MLN, MAX(Leg_Number) MxLN, Flight_Number
FROM Flight_Leg Group by Flight_Number) Z
INNER JOIN Flight_Leg D
on D.Flight_Number = Z.Flight_Number
and D.Leg_Number = Z.MLN
INNER JOIN Flight_Leg A
on A.Flight_Number = Z.Flight_Number
and A.Leg_Number = Z.MxLN
INNER JOIN AirPort DA
on DA.AirPort_Code = D.Departure_AirPort_Code
INNER JOIN AirPort AA
on AA.AirPort_Code = A.Arrival_AirPort_Code
http://sqlfiddle.com/#!2/263a2/56
Not entirely sure if this is what you're after. It's written in MS SQL so the syntax will need some minor tweaks.
SELECT fl.Flight_number,
ao.Name,
ai.Name,
(select min(Leg_number) from FLIGHT_LEG fa where fl.Flight_number
= fa.Flight_number) as min_leg_number,
(select max(Leg_number) from FLIGHT_LEG fb where fl.Flight_number
= fb.Flight_number) as max_leg_number
FROM Flight_leg Fl
inner join AIRPORT as ao on fl.Departure_airport_code =
ao.Airport_code
inner join AIRPORT as ai on fl.Arrival_airport_code =
ai.Airport_code

SQL Query in Linq with HAVING SUM

I have a small database example with three tables: Cities, Country and Geo_Languages (Languages spoken in the countries). Now I want to get the most spoken languages of all cities in the world with a population of at least one million people.
Here is the SQL-Query:
SELECT SUM(c.population) AS totalPop, g.name_en
FROM cities c, country cy, geo_languages g
WHERE c.country_code = cy.id AND
cy.id = g.code2l
GROUP BY g.name_en
HAVING SUM(c.population) > 1000000
ORDER BY totalPop DESC;
Here the Linq-Query so far:
Var query =
from c in db.City
join country in db.Country on c.country_code equals country.id
join languages in db.geo_languages on country.id equals
languages.code2l
group languages by languages.name_en
select new{
totalPop = c.Sum (c => c.population)
};
I just don't know how to convert the HAVING SUM and the ORDER BY into Linq.
I'm thankful for any help.
Try that one:
var query =
from c in db.City
join country in db.Country on c.country_code equals country.id
join languages in db.geo_languages on country.id equals
languages.code2l
group c by languages.name_en into g
where g.Sum(x => x.population) > 1000000
select new {
totalPop = g.Sum(x => x.population)
};

Speedier alternative to SUBSTRING() in MySQL?

I have a query that uses SUBSTRING() as a criteria:
SELECT p.name p_name,
pa.line1 p_line1,
pa.zip p_zip,
c.name c_name,
ca.line1 c_line1,
ca.zip c_zip
FROM bank b
JOIN import_bundle ib ON ib.bank_id = b.id
JOIN generic_import gi ON gi.import_bundle_id = ib.id
JOIN account_import ai ON ai.generic_import_id = gi.id
JOIN account a ON a.account_import_id = ai.id
JOIN account_address aa ON aa.account_id = a.id
JOIN address ca ON aa.address_id = ca.id
JOIN address pa ON pa.zip = ca.zip OR (pa.zip = ca.zip AND pa.line1 = ca.line1)
JOIN prospect p ON p.address_id = pa.id
JOIN customer c ON a.customer_id = c.id
WHERE b.name = 'M'
AND ib.active = 1
AND gi.active = 1
AND SUBSTRING(p.name, 1, 12) = SUBSTRING(c.name, 1, 12)
LIMIT 100
As you can see, it's just comparing the first 12 characters of p.name and c.name. Unfortunately, adding this query to the WHERE clause makes my query unbearably slow. Are there any tricks out there to do this same comparison, or is my best bet to add another column to each table that contains the first 12 characters of the customer's name? I hope it's not the latter because that would be a lot of work and I'll ultimately be doing several comparisons like this.
Add the extra columns and set up an update trigger to populate them automatically. Be sure to create indexes on the new columns, of course.