I keep getting an error saying that a specific column, doesn't exist for b.BookCode But I am fully aware that it does exist! I just wanted to make sure after staring at this for so long that I am not missing something obvious.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM (Inventory i, Author a, Book b)
WHERE (i.BookCode = b.BookCode AND b.AuthorNum = a.AuthorNum);
I'm very new to SQL so I'm unsure if my syntax is off, Also do I need parentheses around the columns that I mentioned in SELECT. I had parentheses around them at first and got an error and was confused as to why.
Thanks!
You are using the old join syntax, you should use INNER JOIN instead.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i
INNER JOIN Author a
USING (BookCode) -- You can also use ON, but USING remove the ambiguous columns
INNER JOIN Book b
USING (AuthorNum); -- Same thing here
If you get an error saying one column doesn't exist, well, you should double check it. MySQL isn't lying to you for the sake of it! You might do your query on an outdated database, for instance.
Minimally, you should change this to:
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i, Author a, Book b
WHERE i.BookCode = b.BookCode AND
b.AuthorNum = a.AuthorNum;
... and this is assuming that the columns in the SELECT part of your select statement are not ambiguous (i.e., found in more than one of the tables in the FROM part).
Related
SELECT collegename(SELECT allotement.collegename,dean.id
FROM dean,allotement
WHERE allotement.city=dean.city
&&dean.collegename<>allotement.collegename
&&dean.id<>allotement.id)as t WHERE id=1
SELECT collegename from (
SELECT allotement.collegename, dean.id
FROM dean,allotement WHERE allotement.city=dean.city
and dean.collegename<>allotement.collegename
and dean.id<>allotement.id)
as t WHERE id=1
A few points to note here:
Treat sub-query as a table source from which you are retrieving the data. Thus, you need a from in the first line.
&& doesn't work in SQL. You have to write and instead.
In your case, writing as t is optional.
You can actually go through a pretty good link which I generally use to follow mySQL syntax, as it's a bit confusing, considering the fact that different SQL databases have a slight variation in syntax and functions available.
You can refer to the official mySQL docs here as well, if in case required.
TRY THIS: We can simply achieve that in following simple way even we don't need sub query for that:
SELECT a.collegename, d.id
FROM dean AS d
INNER JOIN allotement AS a ON a.city = d.city
AND d.collegename <> a.collegename
AND d.id <> a.id
WHERE d.id = 1
I have this SQL query that has been working great. I would like to have something similar that would delete a line from PRC_FIX when the column DESCR in IM_ITEM begins with Clearance instead of where ITEM_VEND_NO = 'GAMES WORK'.
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND `IM_ITEM`.`ITEM_VEND_NO` = 'GAMES WORK'
Thanks for your help.
Edit: This was marked as a possible duplicate. I don't know that looking at the suggested duplicate would have helped me because I wouldn't have known how to implement it in this scenario involving 2 tables, but I'm willing to admit that might be my fault due to me being new to SQL.
You can use
DELETE PRC_FIX
FROM PRC_FIX
INNER JOIN IM_ITEM
ON IM_ITEM.ITEM_NO = PRC_FIX.ITEM_NO
WHERE UPPER(IM_ITEM.DESCR) LIKE 'CLEARANCE%';
You need to use the wildcard %.
in order to match with this string with different string which begins with "Clearance" you need to use "Clearance%".
Look here: SQL like search string starts with
You're fixed code:
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND IM_ITEM.DESCR LIKE 'Clearance%'
DETELE FROM PRC_FIX WHERE ITEM_NO IN (SELECT ITEM_NO FROM IM_ITEM WHERE ITEM_VEND_NO` = 'GAMES WORK')
EDIT OK so the main problem here was initial column1 FROM table1 with the join. Even that column1 has to be fully defined as table1.column1 even tho it is next to the FROM which seems at best odd to me. But I guess this is a newb error and I hope other newbs will find this useful.
//========================================================================
Have used simple joins before without problems. I thought the table.column format was unambiguous.
Warning is:
Integrity constraint violation: 1052 Column 'transmissionProgramID'
in field list is ambiguous'
The SQL is:
SELECT transmissionProgramID FROM transmissionProgramOwner
JOIN transmissionProgram on transmissionProgram.transmissionProgramID
= transmissionProgramOwner.transmissionProgramID WHERE
ownerType = '$ownerType' AND ownerID = '$ownerID' ORDER BY startDate
The two table transmissionProgramOwner and transmissionProgram both have fields called transmissionProgramID. I just cannot see how the table.column leaves anything ambiguous.
Sure it is something simple but I cannot see it. And I apologize for the long variable names but helps me keep things clear.
Additional info: Both transmissionProgramID are set to unique in both tables. I have tried every flaovor of JOIN but I think a simple join is allowed which just returns all records that match... In any case have tried every type of join just to make sure.
Friend try this
SELECT t1.transmissionProgramID FROM transmissionProgramOwner t1
JOIN transmissionProgram t2 on t2.transmissionProgramID
= t1.transmissionProgramID WHERE
t1.ownerType = '$ownerType' AND t1.ownerID = '$ownerID' ORDER BY t1.startDate;
change to
SELECT transmissionProgram.transmissionProgramID FROM
transmissionProgramOwner JOIN transmissionProgram on
'transmissionProgram.transmissionProgramID'
= 'transmissionProgramOwner.transmissionProgramID' WHERE ownerType = '$ownerType' AND ownerID = '$ownerID' ORDER BY startDate
I know this has been asked plenty times before, but I cant find an answer that is close to mine.
I have the following query:
SELECT c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName, c.cases_timestamp, o.organisation_name
FROM db_cases c, db_custinfo ci, db_organisation o
WHERE c.userInfo_ID = ci.userinfo_ID AND c.cases_status = '2'
AND organisation_name = (
SELECT organisation_name
FROM db_sites s, db_cases c
WHERE organisation_ID = '111'
)
AND s.sites_site_ID = c.sites_site_ID)
What I am trying to do is is get the cases, where the sites_site_ID which is defined in the cases, also appears in the db_sites sites table alongside its organisation_ID which I want to filter by as defined by "organisation_ID = '111'" but I am getting the response from MySQL as stated in the question.
I hope this makes sense, and I would appreciate any help on this one.
Thanks.
As the error states your subquery returns more then one row which it cannot do in this situation. If this is not expect results you really should investigate why this occurs. But if you know this will happen and want only the first result use LIMIT 1 to limit the results to one row.
SELECT organisation_name
FROM db_sites s, db_cases c
WHERE organisation_ID = '111'
LIMIT 1
Well the problem is, obviously, that your subquery returns more than one row which is invalid when using it as a scalar subquery such as with the = operator in the WHERE clause.
Instead you could do an inner join on the subquery which would filter your results to only rows that matched the ON clause. This will get you all rows that match, even if there is more than one returned in the subquery.
UPDATE:
You're likely getting more than one row from your subquery because you're doing a cross join on the db_sites and db_cases table. You're using the old-style join syntax and then not qualifying any predicate to join the tables on in the WHERE clause. Using this old style of joining tables is not recommended for this very reason. It would be better if you explicitly stated what kind of join it was and how the tables should be joined.
Good pages on joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html (for the right syntax)
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html (for the differences between the types of joins)
I was battling this for an hour, and overcomplicated it completely. Sometimes a quick break and writing it out on an online forum can solve it for you ;)
Here is the query as it should be.
SELECT c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName, c.cases_timestamp, c.sites_site_ID
FROM db_cases c, db_custinfo ci, db_sites s
WHERE c.userInfo_ID = ci.userinfo_ID AND c.cases_status = '2' AND (s.organisation_ID = '111' AND s.sites_site_ID = c.sites_site_ID)
Let me re-write what you have post:
SELECT
c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName,
c.cases_timestamp, c.sites_site_ID
FROM
db_cases c
JOIN
db_custinfo ci ON c.userInfo_ID = ci.userinfo_ID and c.cases_status = '2'
JOIN
db_sites s ON s.sites_site_ID = c.sites_site_ID and s.organization_ID = 111
I'm new to joins and I'm sure this is ridiculously simple. If I remove one join in the query the remainder of the query works regardless of which join I remove. But as shown it gives the error saying the column doesn't exist. Any pointers?
select
loc_carr.address1 as carr_addr1,
loc_cust.address1 as cust_addr1
from db_name.carrier, db_name.customer
join db_name.location as loc_carr on vats.carrier.location_id=loc_carr.location_id
join db_name.location as loc_cust on vats.customer.location_id=loc_cust.location_id
thanks
I'll take a guess that there is a column named something like carrier_id that can be used to join the carrier and customer tables. Given that assumption, try this:
select
loc_carr.address1 as carr_addr1
, loc_cust.address1 as cust_addr1
from vats.carrier as a
join vats.customer as b
on b.carrier_id=a.carrier_id
join vats.location as loc_carr
on loc_carr.location_id=a.location_id
join vats.location as loc_cust
on loc_cust.location_id=b.location_id
Notice the use of aliases for the table references to make things easier to read. Also note how I'm using explicit SQL join syntax (instead of listing tables separated by commas).
#Bob Duell has the solution for your problem. To understand better why this error is produced, notice that in the FROM clause, you "join" tables using both explicit JOIN syntax and the implicit joins with comma: , which is (almost) equivalent to a CROSS JOIN. The precedence however of JOIN is stronger than the comma , operator. So, that part is parsed like this:
FROM
( db_name.carrier )
,
( ( db_name.customer
JOIN db_name.location AS loc_carr
ON carrier.location_id = loc_carr.location_id -- this line
) -- gives the error
JOIN join db_name.location AS loc_cust
ON customer.location_id = loc_cust.location_id
)
In the mentioned line above, the vats.carrier.location_id throws the error, as there is no carrier table in that scope (inside that parenthesis).