SELECT statement with spaces between tables? - mysql

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.

Related

SQL Query Differences between MS Access and MySQL

I'm having trouble converting a MS Access Query into a MySQL view. It seems that in Access, you're able to use a field that's created within the same select statement later on in the select statement. Whereas, in MySQL, that seems to not be the case.
e.g. Access Generated Code
SELECT IIf([Bonus]>0,[Base Salary],0) AS [BE Base], [BE Base]+[Bonus] AS [BE TC]
FROM titles_tbl INNER JOIN wage_tbl ON titles_tbl.Job_Title = wage_tbl.[Standard Title]
WHERE (((titles_tbl.Mod_Code)="3B") AND ((wage_tbl.Bonus)>0));
vs MySQL Converted Code
select if(`bonus`>0,`base salary`,0) as `be base`, `be base`+`bonus` as `be tc`
from titles_tbl inner join wage_tbl on titles_tbl.job_title = wage_tbl.`standard title`
where (((titles_tbl.mod_code)="3b") and ((wage_tbl.bonus)>0));
When I put this Access-generated SQL code into MySQL (after converting it appropriately), I get an error saying
10:17:07 Error Code: 1054. Unknown column 'be base' in 'field list' 0.062 sec
To me, it seems like MySQL can't use a new column in the same select statement that it's generated in.
Does anyone know whether that's the case, or what the real problem might be (as well as a solution)? Thank you in advance!
Your MySQL code should be
select if(`bonus`>0,`base salary`,0) as `be base`,
((SELECT `be base`) + `bonus`) as `be tc`
from ...
Firstly, if you use spaces in aliases you should surround the alias with ' (quote) or ` (backticks)
Secondly, if you want to refer, in a SELECT, to an alias of the same SELECT, you have to (SELECT `the_alias`)
And thirdly, in the subselect it is mandatory to wrap the aliased column with ` (backticks), simple quotes won't work there.

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

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.

Unknown column in 'field list' error on MySQL Update query

I keep getting MySQL error #1054, when trying to perform this update query:
UPDATE MASTER_USER_PROFILE, TRAN_USER_BRANCH
SET MASTER_USER_PROFILE.fellow=`y`
WHERE MASTER_USER_PROFILE.USER_ID = TRAN_USER_BRANCH.USER_ID
AND TRAN_USER_BRANCH.BRANCH_ID = 17
It's probably some syntax error, but I've tried using an inner join instead and other alterations, but I keep getting the same message:
Unknown column 'y' in 'field list'
Try using different quotes for "y" as the identifier quote character is the backtick (`). Otherwise MySQL "thinks" that you point to a column named "y".
See also MySQL 8 Documentation
Please use double-/single quotes for values, strings, etc.
Use backticks for column-names only.
Enclose any string to be passed to the MySQL server inside single quotes, e.g.:
$name = "my name"
$query = " INSERT INTO mytable VALUES ( 1 , '$name') "
Note that although the query is enclosed between double quotes, you must enclose any string in single quotes.
You might check your choice of quotes (use double-/ single quotes for values, strings, etc and backticks for column-names).
Since you only want to update the table master_user_profile I'd recommend a nested query:
UPDATE
master_user_profile
SET
master_user_profile.fellow = 'y'
WHERE
master_user_profile.user_id IN (
SELECT tran_user_branch.user_id
FROM tran_user_branch WHERE tran_user_branch.branch_id = 17);
Just sharing my experience on this. I was having this same issue. The insert or update statement is correct. And I also checked the encoding. The column does exist.
Then! I found out that I was referencing the column in my Trigger.
You should also check your trigger see if any script is referencing the column you are having the problem with.
In my case, it was caused by an unseen trailing space at the end of the column name. Just check if you really use "y" or "y " instead.
While working on a .Net app build with EF code first, I got this error message when trying to apply my migration where I had a Sql("UPDATE tableName SET columnName = value"); statement.
Turns out I misspelled the columnName.
If it is hibernate and JPA. check your referred table name and columns might be a mismatch
Just sharing my experience on this. I was having this same issue. My query was like:
select table1.column2 from table1
However, table1 did not have column2 column.
In my case, the Hibernate was looking for columns in a snake case, like create_date, while the columns in the DB were in the camel case, e.g., createDate.
Adding
spring:
jpa:
hibernate:
naming: # must tell spring/jpa/hibernate to use the column names as specified, not snake case
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
to the application.ymlhelped fix the problem.
In my case, I used a custom table alias for the FROM table, but I used the default table alias (MyTable) in the field list instead of the custom table alias (t1). For example, I needed to change this...
mysql> SELECT MyTable.`id` FROM `MyTable` t1;
...to this...
mysql> SELECT t1.`id` FROM `MyTable` t1;
In my case I had misspelled the column name in the table's trigger. Took me a while to connect the error message with the cause of it.
I too got the same error, problem in my case is I included the column name in GROUP BY clause and it caused this error. So removed the column from GROUP BY clause and it worked!!!
I got this error when using GroupBy via LINQ on a MySQL database. The problem was that the anonymous object property that was being used by GroupBy did not match the database column name. Fixed by renaming anonymous property name to match the column name.
.Select(f => new
{
ThisPropertyNameNeedsToMatchYourColumnName = f.SomeName
})
.GroupBy(t => t.ThisPropertyNameNeedsToMatchYourColumnName);
A query like this will also cause the error:
SELECT table1.id FROM table2
Where the table is specified in column select and not included in the from clause.