I've read the other MySQL reserved words questions that suggest using backticks when calling reserved words in MySQL.
Unfortunately, I've tried those and they haven't worked in my particular case where multiple tables/table aliases are being used. My query looks like:
select rf.id, `s.Status` as 'Status'
from form rf
left outer join sharerequest rr on rr.formId = rf.id
left outer join schedule s on s.sharerequestid = rr.id
I've tried different variations of this (backticks around just the word "status", encapsulating it in brackets (like SQLServer)) and nothing has worked.
I'm pretty sure backticks are how I'm supposed to handle this but I'm not sure what I'm doing wrong here.
For the record, this is covered in the MySQL manual here:
https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
Related
I'm trying to get results when both tables have the same machine number and there are entries that have the same number in both tables.
Here is what I've tried:
SELECT fehler.*,
'maschine.Maschinen-Typ',
maschine.Auftragsnummer,
maschine.Kunde,
maschine.Liefertermin_Soll
FROM fehler
JOIN maschine
ON ltrim(rtrim('maschine.Maschinen-Nr')) = ltrim(rtrim(fehler.Maschinen_Nr))
The field I'm joining on is a varchar in both cases. I tried without trims but still returns empty
I'm using MariaDB (if that's important).
ON ltrim(rtrim('maschine.Maschinen-Nr')) = ltrim(rtrim(fehler.Maschinen_Nr)) seems wrong...
Is fehler.Maschinen_Nr really the string 'maschine.Maschinen-Nr'?
SELECT fehler.*, `maschine.Maschinen-Typ`, maschine.Auftragsnummer, maschine.Kunde, maschine.Liefertermin_Soll
FROM fehler
JOIN maschine
ON ltrim(rtrim(`maschine.Maschinen-Nr`)) = ltrim(rtrim(`fehler.Maschinen_Nr`))
Last line compared a string to a number. This should be doing it.
Also, use the backtick to reference the column names.
The single quotes are string delimiters. You are comparing fehler.Maschinen_Nr with the string 'maschine.Maschinen-Nr'. In standard SQL you would use double quotes for names (and I think MariaDB allows this, too, certain settings provided). In MariaDB the commonly used name qualifier is the backtick:
SELECT fehler.*,
`maschine.Maschinen-Typ`,
maschine.Auftragsnummer,
maschine.Kunde,
maschine.Liefertermin_Soll
FROM fehler
JOIN maschine
ON trim(`maschine.Maschinen-Nr`) = trim(fehler.Maschinen_Nr)
(It would be better of course not to use names with a minus sign or other characters that force you to use name delimiters in the first place.)
As you see, you can use TRIM instead of LTRIM and RTRIM. It would be better, though, not to allow space at the beginning or end when inserting data. Then you wouldn't have to remove them in every query.
Moreover, it seems Maschinen_Nr should be primary key for the table maschine and naturally a foreign key then in table fehler. That would make sure fehler doesn't contain any Maschinen_Nr that not exists exactly so in maschine.
To avoid this problems in future, the convention for DB's is snake case(lowercase_lowercase).
Besides that, posting your DB schema would be really helpfull since i dont guess your data structures.
(For friendly development, is usefull that variables, tables and columns should be written in english)
So with this, what is the error that you get, because if table "maschine" has a column named "Maschinen-Nr" and table "fehler" has a column named "Maschinen_Nr" and the fields match each other, it should be correct
be careful with Maschinen-Nr and Maschinen_Nr. they have - and _ on purpose?
a very blind solution because you dont really tell what is your problem or even your schema is:
SELECT table1Alias.*, table2Alias.column_name, table2Alias.column_name
FROM table1 [table1Alias]
JOIN table2 [table2Alias]
ON ltrim(rtrim(table1Alias.matching_column)) = ltrim(rtrim(table2Alias.matching_column))
where matching_columns are respectively PK and FK or if the data matches both columns [] are optional and if not given, will be consider table_name
I have been working on a database for my coursework and have used phpMyAdmin to build it. Now I am working on the queries using the query tool.
When I pick the tables and data I want to query and press "update query" it will generate the query which looks something like this:
SELECT `Customer`.`CustomerName`, `OrderDetails`.`Product`, `OrderDetails`.`QuantityOrdered`
FROM `Customer`
LEFT JOIN `Order` ON `Order`.`Customer` = `Customer`.`CustomerID`
LEFT JOIN `OrderDetails` ON `OrderDetails`.`Order` = `Order`.`OrderID`
This works fine and gives me the results I was expecting. However when I try and write my own query and put something like "SELECT Customer.CustomerName," WITHOUT the ' symbol it won't work and just throws up an error message.
Must I always wrap them using the ' symbol for the query to work?
Forward quotes are used to escape object names in MySQL. You don't have to use them unless you use names that wouldn't be valid identifiers - in this case, the table name order is a reserved word, and must be escaped. All the other tables and columns you're using seem to be OK.
Except for the visual nightmare and ability to create horrendous table names, backticks are entirely unnecessary. You will, however, be required to wrap any variables in single quotes.
As you can see from my example below, using backticks is not a requirement with PHPMYADMIN;
The reason it is not working when you remove the backticks is because you have a column called 'order'. Order is a keyword in SQL and therefore cannot be used as a column name without being wrapped in either quotes or backticks.
I wanna join two buckets by below query. But it throws me an error like below. What am I missing?
Error:
syntax error - at ebook_id
Query:
SELECT ebook_id, COUNT(*) as count
FROM `log` USE KEYS `log.`ebook_id
JOIN `product` USE KEYS product.book_id
WHERE meta(`log`).id LIKE 'ebook_page_request%' AND `log`.date = CLOCK_LOCAL('1111-11-11') GROUP BY log.ebook_id
FROM `log` USE KEYS `log.`ebook_id
The back tick location is incorrect it needs to end before dot Like below
FROM `log` USE KEYS `log`.ebook_id
USE KEYS on LEFT side of JOIN needs to be CONSTANT and right of side of JOIN can't use USE KEYS and it must be ON KEYS with expression referring from left side bucket
ANSI JOINS are supported only in CB 5.50
Pre CB 5.50 supports LOOKUP and Index Joins
Actually, it's usually not necessary to use the back-ticks at all. Just use the plain name of the bucket or field.
The back-ticks are escapes, used to allow you to refer to buckets and fields that contain characters that are not allowed by default in identifiers in N1QL. The most notable of these special characters is - (the minus sign), which is unfortunately used in Couchbase's example data sets, like beer-sample.
By default, do not use `` back ticks.
I have a keywords column and it contains stuff like this:
apples, oranges, pine apple
I'm trying to change the spaces to hyphens using this query"
UPDATE articles SET keywords = REPLACE(keywords," ","-") WHERE
keywords REGEXP '[A-Z] [A-Z]' limit 1;
But this adds hyphens where I don't want them, like this:
apples,-oranges,-pine-apple
Can this be done with REGEXP? Or will I need to involve PHP?
Thank you.
You're selecting rows based on the regular expression, but how does REPLACE() know about that? It's going to replace spaces with hyphens, just like you told it.
There are a few options for adding regexp-based search and replace in MySQL such as a UDF, and MariaDB supports it natively:
UPDATE articles SET keywords = REGEXP_REPLACE(keywords, "[A-Z] [A-Z]", "-");
Also worth mentioning that a LIMIT clause without an ORDER BY clause is not very helpful.
In a statement like this;
$sql = "SELECT distinct `term`,count(*) as count
FROM {$temp_table_name}
group by `term` order by count DESC";
What does using the back tick character (`) around the field name 'term' buy me?
Is the usage of back ticks for performance reasons? Or is it for some sort of a SQL injection protection?
Note: After I submit the question, I realized that the backtick character does not show around the field name 'term' - right here on stackoverflow.
I don't know of a way of making it appear here in the question body.
If term is mysql key word, you need to quote it by `, otherwise, it is not necessary.
Ps: distinct is not necessary in your case, because you group by it.
The back-tick is the 'official' identifier quote character.
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
It allows a wider array of characters in an identifier, as described on the linked documentation.
Backticks just allow the use of spaces or other alternate characters in field names.
I think it's already been pretty well explained here.
When We use a keyword as a table name,field-name in MySQL use backticks, or double-quotes when ANSI_QUOTES is enabled.Other wise it is not necessary.It is not releated to SQL injection protection