Ambiguous left join? - mysql

so I'm struggling with what I'm guessing is a very simple problem. I've searched around a bit, but none of the solutions I've found so far have worked on my problem.
SELECT arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
This naturally produces the "ambiguous error", since the column 'arrangement_ID' is present in both tt_arrangement and tt_vaktliste_vakt. Thinking this was easy to fix, I made the following changes:
SELECT **arrangement_ID.tt_arrangement**, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
However, this produced the error "column doesn't exist". And that's where I'm stuck.
Not sure if it matters, but when using SELECT * the query works as intended. Though that is not really an option for what I'm going to use the query for.
In advance, thanks for any replies.

Prefix the ambiguous column name with the tablename:
SELECT t_arrangement.arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
(assumes hva, dato are unique column names)
(You can also use aliases, but will still need to prefix ambiguous column names with alias)

You need to give your table names an alias, like below.
SELECT a.arrangement_ID, a.hva, a.dato
FROM tt_arrangement AS a
LEFT JOIN tt_vaktliste_vakt AS v
ON (a.arrangement_ID = v.arrangement_ID)
Not sure is the top lines right as i don't know you table structure so can't know which column comes from where.
Hopes this helps.

Related

Naming columns from other tables

Just a quick question about naming columns that come from other tables, below i have the tables put in the SQL statement but after it I put an abbreviated version "MO" is this correct/ will this work in all situations or should i just stick to the full version like module.mod_code?
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO, SYLLABUS SY, PROGRAMME PR
WHERE MO.MOD_CODE = SY.MOD_CODE
AND SY.PROG_CODE = PR.PRO_CODE
AND PR.NFQ_LEVEL = ‘LEVEL 9’
AND MO.DESCRIPTION LIKE ‘%RESEARCH%’ OR DESCRIPTION LIKE ‘%QUALATIVE%’ OR DESCRIPTION LIKE ‘%QUANTITATIVE%’;
Thanks :)
If I understood correctly, you're trying to reference columns using the table alias, and are wondering if there is any difference in using MO.[column] and module.[column]?
If that is the case, it is preferred to use the table alias to reference the column. This is because you may join back to the same table to retrieve a different subset of data. If you do this, you will need to define which set you want the data to come from.
Module AS M ---- Programme AS P ------ Module AS SUBM
You cannot stick to the full version. Once you have given a table or subquery an alias, that is the name of that object in the scope of the query. Actually, what happens is that the table name becomes the table alias, so you can use it for qualifying columns in the table.
You should also learn proper explicit JOIN syntax. I am also guessing that you are missing parentheses on your WHERE clause:
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO JOIN
SYLLABUS SY
ON MO.MOD_CODE = SY.MOD_CODE JOIN
PROGRAMME PR
ON SY.PROG_CODE = PR.PRO_CODE
WHERE PR.NFQ_LEVEL = 'LEVEL 9' AND
(MO.DESCRIPTION LIKE '%RESEARCH%' OR
MO.DESCRIPTION LIKE '%QUALATIVE%' OR
MO.DESCRIPTION LIKE '%QUANTITATIVE%'
);
If you attempted something like SELECT MODULE.MOD_CODE in this query, it would return an error, because the table alias MODULE is not assigned to any object.

No tables used?

I've been working with this bit of code for some time now, and now, after some tinkering, I thought I finally got it to work, then it gives me this error:
Error Number: 1096
No tables used
SELECT *
and this is the code
SELECT start.*, posts.did, COUNT(posts.pid)
FROM akia_starting_posts AS start
JOIN posts
ON posts.did = start.did
JOIN akia_users AS users
ON users.username = start.username
I'm pretty sure at the * my start is being used, so what is it talking about? It couldn't be any other code in the file, since when I take out this bit of code, it starts working.
Try to add backticks on sql string that has the word start on it. To begin try to use the one table in question first like.
SELECT 'start.*' FROM akia_starting_posts as 'start'
and if it works then you may incorporate it in fullscale join like the one you posted
with bacticks used.

MySQL Error 1172 - Result consisted of more than one row

I'm getting this error from MySQL when running a query inside a stored procedure:
Error Code: 1172
Result consisted of more than one row
I understand the error: I'm doing a SELECT (...) INTO (var list), and thus the query is required to return a single row. When I use LIMIT 1 or SELECT DISTINCT, the error goes away.
However: when I run the original query manually (without LIMIT or DISTINCT), it does return a single row. So I'm suspecting I may have bumped into a MySQL bug. Does anyone know what could be happening?
EDIT
I'm posting the SQL as requested. Everything that starts with an underscore is a variable declared earlier inside the procedure. When I test it, I'm replacing _cd_pai_vc with the ID for the record that is causing the problem.
SELECT a.valor, IFNULL(p.valor, 0), fn_cd2alias(ra.cd_registro), fn_cd2alias(IFNULL(p.valor,0))
INTO _valor, _cd_pai_vc, _alias_verbete, _alias_pai
FROM dados_registros ra
INNER JOIN dados_varchar255 a
ON a.cd_registro = ra.cd_registro
AND a.fl_excluido = 0
AND a.alias = 'vc-verbetes-termo'
LEFT OUTER JOIN dados_registros rp
INNER JOIN dados_int p
ON p.cd_registro = rp.cd_registro
AND p.fl_excluido = 0
AND p.alias = 'vc-remissoes-termo referenciado'
INNER JOIN dados_int pt
ON pt.cd_registro = rp.cd_registro
AND pt.fl_excluido = 0
AND pt.alias = 'vc-remissoes-tipo remissao'
AND fn_cd2alias(pt.valor) = 'hierarquica'
ON ra.cd_registro = rp.cd_entidade
AND rp.fl_excluido = 0
AND fn_cd2alias(rp.cd_modulo) = 'vc-remissoes'
WHERE ra.cd_registro = _cd_pai_vc
AND ra.fl_excluido = 0;
I had the similiar issue and when I put table alias it worked like a charm.
SELECT t.tax_amount,t.tax_percentage FROM nepse_tax t
I had this problem and found it went away when I used both table name and column name in select statements, even simple ones.
The issue i had was IN parameter and the column name both were same so altered IN parameter name and it worked
I have experienced the same error in mysql.
MySQL Error 1172 - Result consisted of more than one row
Then I saw the question:
mysql stored procedure error (1172, 'Result consisted of more than one row')
But it was not I meant to ask. LIMIT 1;-wasn't up to my expectation. It will just return the first row for all the case.
Then I started looking at this one deeply and now I got the solution.
This case happend because the code for stored procedure returns multiple rows and that was because I had many extra spaces and tab characters in my code[the code I wrote for stored procedure] and when I removed them with just one/two appropriate tab chars- it was just like a flying machine.
I don't know if it is the same case that you are experienced with. Anyway give a try.
Thank you.
I had the same problem, the solution by Nava Bogatee worked like a charm,
below is a part of the procedure
select attendance
into brb
from Stud
where Rno like rno;
This didn't work I have no idea why.
Then I changed this to
select t.attendance
into brb
from Stud t
where t.Rno like rno;
and it worked.
I guess due to some reason the Rno column is returning all the values in the table, that is why the multiple row error.

SQL unknown column when doing join

I have been getting an error "Unknown column 'guests_guest.id' in 'field list'" when i try to run the following:
SELECT guests_guest.id
FROM `guests_guest` full join
guests_guest_group
on guests_guest.id=guests_guest_group.guest_id
All the column& table names are correct. in fact, running just
SELECT guests_guest.id
FROM `guests_guest`
works just fine. I suspect there is a syntax issue I am missing. what am I doing wrong?
try:
SELECT gg.id
FROM `guests_guest` as gg
join guests_guest_group as ggg
on ggg.guest_id=gg.id
assuming guests_guest_group does not have an id column.
full join ?
Have you tried simply removing the full?
This not Oracle, it's MySQL, right? AFAIK, FULL JOIN is not implemented yet in MySQL.
The parser (because "full' is not a keyword it knows), evaluates your query as:
SELECT guests_guest.id
FROM guests_guest AS full <--- crucial note
JOIN guests_guest_group
ON guests_guest.id = guests_guest_group.guest_id
After that, guests_guest is not a name it knows, but it uses full as an alias for table guests_guest. That's why this error is produced.
If you really need FULL JOIN and not (INNER) JOIN, then search SO for how to implement FULL JOIN in MYSQL.
#rockerest: I guest so.
Two things I'd look at:
Spelling... I am a fast typer, but sometimes my fingers are dyslexic. Worst case, do a describe on each table and check column names against each other. Or, use the system-confessed column names and copy/paste.
Aliases... but, someone else has mentioned that.
So I guessed just one thing.

mysql group_concat in where

I am having a problem with the following query(if this is a duplicate question then i'm terribly sorry, but i can't seem to find anything yet that can help me):
SELECT d.*, GROUP_CONCAT(g.name ORDER BY g.name SEPARATOR ", ") AS members
FROM table_d AS d LEFT OUTER JOIN table_g AS g ON (d.eventid = g.id)
WHERE members LIKE '%p%';
MySQL apparently can't handle a comparison of GROUP_CONCAT columns in a WHERE clause.
So my question is very simple. Is there a workaround for this, like using sub-query's or something similar? I really need this piece of code to work and there is not really any alternative to use other than handling this in the query itself.
EDIT 1:
I won't show the actual code as this might be confidential, I'll have to check with my peers. Anyway, I just wrote this code to give you an impression of how the statement looks like although I agree with you that it doesn't make a lot of sense. I'm going to check the answers below in a minute, i'll get back to you then. Again thnx for all the help already!
EDIT 2:
Tried using HAVING, but that only works when i'm not using GROUP BY. When I try it, it gives me a syntax error, but when I remove the GROUP BY the query works perfectly. The thing is, i need the GROUP BY otherwise the query would be meaningless to me.
EDIT 3:
Ok, so I made a stupid mistake and put HAVING before GROUP BY, which obviously doesn't work. Thanks for all the help, it works now!
Use HAVING instead of WHERE.
... HAVING members LIKE '%peter%'
WHERE applies the filter before the GROUP_CONCAT is evaluated; HAVING applies it later.
Edit: I find your query a bit confusing. It looks like it's going to get only one row with all of your names in a single string -- unless there's nobody in your database named Peter, it which case the query will return nothing.
Perhaps HAVING isn't really what you need here...
Try
SELECT ...
...
WHERE g.name = 'peter'
instead. Since you're just doing a simple name lookup, there's no need to search the derived field - just match on the underlying original field.
GROUP_CONCAT is an aggregate function. You have to GROUP BY something. If you just want all the rows that have %peter% in them try
SELECT d.*, g.name
FROM table_d AS d
LEFT OUTER JOIN table_g AS g
ON (d.eventid = g.id)
WHERE g.name LIKE '%peter%';