I am new to databases and i really stuck! Please give me a hand! Have no idea, where I made a mistake...
I have 2 tables patient and caretaker
they both have lastname and firstname
I need to retrieve lastname and firstname from both of them and i made the following query:
SELECT firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname;
But it says
Error Code: 1052. Column 'firstname' in field list is ambiguous 0.034 sec
Do you have any idea why? I will really appreciate your help...
Thanks!
Use Database Objects.
When you specify the firstname in the columns list where both the tables have the same column name, data base engine cannot recognize the first name of which table exactly are you trying to retrieve!!
Many of the above answers says the same thing.
Just to reiterate the same,
SELECT patient.firstname, ctaker.firstname
FROM mortenu8.patient patient, caretaker ctaker
WHERE ctaker.firstname = patient.firstname;
try this:
SELECT patient.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname;
Assuming your query of join is working you can try this
SELECT patient.firstname,caretaker.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname
OR
SELECT caretaker.firstname
FROM mortenu8.patient, caretaker
where caretaker.firstname = patient.firstname
Try this
SELECT MP.firstname as PatientName,
C.firstname as CaretakerName
FROM mortenu8.patient MP, caretaker C
where C.firstname = MP.firstname;
Related
I have troubles with execution sql
any time I execute it gives me an error Ambiguous column name 'salesYTD'
my statement is :
SELECT COUNTRYREGIONCODE, NAME, AVG(SALESQUOTA),AVG(BONUS), AVG(SALESYTD)
FROM SALES.SALESPERSON SP
INNER JOIN SALES.SALESTERRITORY ST
ON SP.TERRITORYID = ST.TERRITORYID
GROUP BY NAME, COUNTRYREGIONCODE;
the name of that column is correct. I don't understand what I am doing wrong. Thanks for any help
This means that SALESYTD is in both tables. I don't know which you want.
When you have more than one table in a query always qualify your column names.
SELECT ST.NAME, ST.COUNTRYREGIONCODE,
AVG(SP.SALESQUOTA), AVG(SP.BONUS), AVG(SP.SALESYTD)
FROM SALES.SALESPERSON SP INNER JOIN
SALES.SALESTERRITORY ST
ON SP.TERRITORYID = ST.TERRITORYID
GROUP BY ST.NAME, ST.COUNTRYREGIONCODE;
I'm just guessing where the columns come from.
Does that column exist in more than one table?
If so, you should name the field like this:
SP.salesYTD
or
ST.salesYTD
Depending on what you want to show.
Good luck.
I need help with a complex select statement in SQL. I have these two table here:
Table user:
Table contacts_from_user:
When I make a select
SELECT name, vorname, gebdat, bezeichnung, wert
FROM user
JOIN contacts ON u_id = user_u_id
I get multiple lines for one user because he has more then one contact options but I need to put it in just one line:
The line should be looks like this:
name, vorname, gebdat, bezeichung_1, wert_1, bezeichnung_2, wert_2.......
How ca I do this?
Thanks a lot!
In pseudo-code, the best way to handle such scenarios is:
query = SELECT A.ID, A.stuff, B.stuff FROM A JOIN B ON A.ID = B.A_ID
results = run query
prev_A_ID = impossible_A_ID
for each result
if prev_A_ID not equal result_A_ID
create new A and set as current A
add B.stuff to current A
set prev_A_ID to result_A_ID
end for
I have this one table which is table staff. The column is id_staff, no_staff and id_posting. I would like to count how many staff using the same id_posting. Can anyone help me with the query?thanks.
Do you have tried to make the query yourself?
This is quite basic
select count(id_staff), id_posting from staff where id_posting = ? group by id_posting
Please try this
SELECT id_posting ,COUNT( `id_staff` ) as cnt_staff
FROM table_staff
GROUP BY id_posting
select COUNT(*) AS ID_COUNT from YOUR_TABLE group by THE_ID_WHICH_IS_REPEATED
http://www.w3schools.com/sql/sql_func_count.asp
check the tutorial for more info.
I am doing a query for a class where I need to select pet owner names based on whether or not they own a Chihuahua.
I have to use two tables, PetOwner and Pet. PetOwner has an owner number (ownerNo), owner name (oLName, oFName), and Pet has owner number and petType. Here is the code I am trying to use:
SELECT
PetOwner.oFName,
PetOwner.oLName
FROM
PetOwner,
Pet
WHERE
PetOwner.ownerNo = (SELECT
Pet.ownerNumber
FROM
Pet
WHERE
Pet.petType = 'Chihuahua'
);
The error says "unknown command beginning "Pet.petTyp..." - rest of line ignored." I know the issue is in the WHERE clause, but I can't seem to tweak it to where it will work, and I am sure the answer is obvious.
I just need a second set of eyes to look it over.
How can i do this ?
Inside your query:
SELECT PetOwner.oFName, PetOwner.oLName
FROM PetOwner, Pet
You have mentioned the table 'Pet' here. Table 'Pet' shouldn't be mentioned here, as you're selecting the oFName and oLName from the table 'PetOwner', not 'Pet'.
A simple join query will give you exactly what you want. No need to use subquery in the above mentioned way. You need to join Pet table with PetOwner table, based on PetOwner.ownerNo = Pet.ownerNumber, under the condition that Pet.petType is 'Chihuahua'.
So the completed query is:
SELECT po.oFname, po.oLName
FROM PetOwner as po
JOIN Pet as p
ON po.ownerNo = p.ownerNumber
WHERE p.petType = 'Chihuahua'
The following query worked for me:
SELECT ownername
FROM PetOwner
WHERE PetOwner.ownerNo = (SELECT Pet.ownerNo FROM Pet WHERE Pet.petType = 'Chihuahua');
Regards.
SELECT oFName, oLName
FROM PetOwner
WHERE ownerNo = (SELECT ownerNumber FROM Pet WHERE petType = 'Chihuahua');
Your query is confusing sql for which Pet to select parent one or child one. Provide alias for child table in your sub query as mentioned below :
SELECT
PetOwner.oFName,
PetOwner.oLName
FROM
PetOwner,
Pet
WHERE
PetOwner.ownerNo = (SELECT
p.ownerNumber
FROM
Pet p
WHERE
p.petType = 'Chihuahua'
);
Can I select like this?
SELECT DISTINCT idClient, idAcc,Description
FROM client, account
WHERE (account.idCliente = client.idCliente
OR account.idCliente is NULL )
Im getting trouble because it show to me duplicated results :x How can I do it ?
Thanks
EDIT:
RESULTS
idClient idAcc Description
1 3 good
1 2 bad
1 3 bad
Note that im getting 2 diferent Descriptions for same idAcc
EDIT2:
I realy need that search by NULL or Not NULL.
You are using an implicit join. Try using an explicit JOIN like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON account.idCliente = client.idCliente
I would rewrite your query to use a LEFT JOIN so that you get all situations where there is a client.idCliente, but also those where the account.idCliente doesn't exist.
Like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON client.idCliente = account.idCliente;