Couchbase N1QL join query- syntax error - couchbase

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.

Related

MySql reserved word in select with alias table name

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`.

Join returns NULL when data that matches is in the table

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

Why do all my SQL queries have to be wrapped using the ` symbol?

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.

Does using strings containing multiple words separated with whitespaces in select statements 'AS' yield any disadvantages?

When working with SQL Statements in general you would usually encounter something like:
SELECT COUNT( * ) AS `rowCount` FROM `someTable`;
Now is there any disadvantage technical or style guide wise if we would write the query as follows:
SELECT COUNT( * ) AS `Row count` FROM `someTable`;
Or:
SELECT COUNT( * ) `rowCount` FROM `someTable`;
vs
SELECT COUNT( * ) `Row count` FROM `someTable`;
I'm asking specifically about whitespaces in the name we give the result column.
Could this maybe generate an issue when using this query within any programming language?
It depends on how you try to access the columns when you fetch them.
In PHP, if you fetch the rows into an object (rather than an associative array), the alias will become the object property. Accessing a property with a space in its name is not as convenient. Instead of
$row->rowCount
you would have to write:
$row->{"Row count"}
But the most common way to access columns in most MySQL APIs is using some kind of associative array or dictionary. In this case, there's not much difference between
$row['rowCount']
and
$row['Row count']
since you have to put the alias name in quotes either way.
If you put whitespace into your identifiers, then you must delimit them every time you use them.
If you don't put whitespace in your identifiers, and also don't use punctuation characters, non-ASCII characters, and don't use SQL reserved words, then you have the option of using the identifer without delimiting it.

SQL exception with 'from' as a column name

I have a table with a column named 'from'. I want to retrieve data from it and so I tried following query.
select title,from,grade from localcourses where title='new';
But I get following exception due to the column name 'from'.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from,grade from localcourses where title='new'
How can I avoid this without renaming the column name? Thank you.
Try --
select `title`,`from`,`grade` from localcourses where `title`='new';
If you are running MySQL in standard (ANSI) mode, use double quotes to "escape" the keyword:
select title,
"from",
grade
from localcourses
where title='new';
If you are running MySQL in non-standard mode (which is still the default if I'm not mistaken), you need to use MySQL's dreaded "backticks:
select title,
`from`,
grade
from localcourses
where title='new';
On MySQL you can use the ` (back apostrophe -- to the left of the 1 key on your keyboard). Use
`from`.
I'll be the first to say it - you should avoid naming tables, columns, triggers, procedures, functions, etc with the names of reserved, action, and other commonly used words in sql and database engine syntax. It only creates confusion such is the case here.
Assuming Oracle try
select title,"from",grade from localcourses where title='new';
In mySQL, you need to enclose the from column in backtick character
select title,`from`,grade from localcourses where title='new'
I suspect the backtick character you are using is not the right one, I am not sure what type of keyboard you have, so it might not send the proper character in.
Try this instead.
select title,localcourses.from,grade from localcourses where title='new'
and see if that helps