SQL unknown column when doing join - mysql

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.

Related

Why is my query wrong?

before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.

SELECT statement with spaces between tables?

I have the following SQL statement:
SELECT pvov.id, pvov.value
FROM ci_product_variant_option_values pvov, ci_product_variant_options pvo
WHERE pvov.product_variant_option_id=pvo.id
AND pvo.product_id='12345'
Now, what I don't understand is what should ci_product_variant_option_values pvov mean, as it has neither any commas or dots. I know that ci_product_variant_option_values is a table in the database, however pvov (probably an abreviation for product variant option values) has no occurrence in the db.
Can someone please explain me the statement?
pvov is the table alias.
Same as saying:
ci_product_variant_option_values AS pvov
Aliases make code cleaner, instead of having to use ci_product_variant_option_values.id you can simply use pvov.id
I'd also suggest moving away from the deprecated syntax by using explicit JOIN criteria:
SELECT pvov.id, pvov.value
FROM ci_product_variant_option_values pvov
JOIN ci_product_variant_options pvo
ON pvov.product_variant_option_id=pvo.id
WHERE ....
pvov is an alias for ci_product_variant_option_values.

LEFT JOIN breaks WHERE Clause

I've recently been required to input more information from my database and I've just LEFT JOIN to help me, it works almost perfectly(it does actually get the right field from the other table) but my WHERE clause is nullified giving the user access to both tables without the restriction of my where clause.
MySQL doesn't crap out any errors, so I'm assuming it's something to do with my where clause or something happened in the join.
SELECT * FROM students
LEFT JOIN courses ON students.appliedforCourse = courses.idNumber
WHERE
students.telephone LIKE '%$var'
OR students.email LIKE '%$var'
OR students.address like'%$var%'
OR (CONCAT(students.firstName,' ',students.lastName) LIKE '%$var%')
AND addedBy ='$userid'
LIMIT $s,limit
The query itself is correct (although really inefficient due to ORs and % % [ indexes will not be used] ).
I would suggest to echo the query, are you sure that $var is evaluated correctly ? Try to run the query directly in mysql (via phpmyadmin for example or using console).
I suspect that simply you did not set $var value. Then condition e.g. students.telephone LIKE '%$var' will become students.telephone LIKE '%' (always true for not null address), which will match every record of the join , exactly what you are getting.

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%';

Ambiguous left join?

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.