I have a module that generates a csv file to feed data to a third party, but want to modify it to use for another company. The column names they want for the csv are different to the ones set up in the current module, such as Product Name instead of product__name.
An example of the sql in the original mod is:
$query = 'select
p.products_id as uuid,
t.type_name as product__type,
d.products_name as product__name,
d.products_description as product__description,
d.products_url as product__url,
d.products_viewed as product__viewed_count,
p.products_quantity as product__quantity,
p.products_model as product__model,
p.products_image as product__image
.....
I tried to change d.products_name as product__name, to d.products_name as Product Name, but it gives me an sql error.
Is there any easy solution to this without having to rewrite the whole module?
since you are using space for your identifier, you should quote them, like so:
d.products_name as `Product Name`
see http://dev.mysql.com/doc/refman/4.1/en/identifiers.html
You can use backquotes to enclose your aliases.
d.products_name as `Product Name`
This is useful to output column names that are mysql keywords or column names with spaces.
Try this:
d.products_name as 'Product Name',
When you want an alias with spacing, you have to treat it as a string.
d.products_name as 'Product Name',
Related
Today in a live learning session with a friend of mine Dumisani Ndubane, we found out a slight change in behavior when using ORDER BY to sort a result set with column alias by using the ``, '' or "" quote types.
'' and "" aren't affect by the sorting, only `` works but all quotes are allowed to do column aliasing. See queries below;
This sort the Full name in ascending order (using `` quotes on column alias):
SELECT
CONCAT_WS(', ', lastName, firstname) `Full name`
FROM
employees
ORDER BY
`Full name`;
This doesn't sort the result set. Note we used '' quotes
SELECT
CONCAT_WS(', ', lastName, firstname) 'Full name'
FROM
employees
ORDER BY
'Full name';
This doesn't sort the result set. Note we used "" quotes
SELECT
CONCAT_WS(', ', lastName, firstname) "Full name"
FROM
employees
ORDER BY
"Full name";
Also, we where using MySQL version mysql Ver 8.0.19 for osx10.13 on x86_64 (Homebrew) on Mac. Is this intentional, is there an explanation to this behavior? Also, why not stick with backticks(``) with MySQL identifiers and '' or "" for string literals. Why mix them?
I think this could be a user experience (UX) improvement for MySQL because the current status quo seems confusing for a newbie trying to learn.
What do the SO community think and thanks for your help in advance.
Single quotes (and, in MySQL, double quotes) stand for literal strings. So 'Full name' is just that: a literal string. Using that for sorting makes no sense, since the value is constant for all rows: as a result, the ordering of rows is undefined, meaning that the database is free to return rows in whatever order it likes.
Instead, use backticks, that are used for identifiers, so the order by refers to the expression aliases in the select clause.
Or better yet, use an alias that does not requires quoting, so you don’t have to worry about this all.
The second example uses
ORDER BY 'Full Name'
This is a string literal, which is a constant value. It does not refer to the column alias of the same characters.
Any ORDER BY of a constant value results in an arbitrary order, because every row has an equal chance of being ordered before any other row. They are all tied.
Double-quoted strings are also treated as string literals by default in MySQL. This is different from ANSI SQL, where double-quotes are identifier delimiters. MySQL does that if you set sql_mode=ANSI or sql_mode=ANSI_QUOTES.
i have this challenge on tree house
We're back in our e-commerce database.
There's a products table with the columns id, name, description and price.
Can you retrieve both the name and description aliased as "Product Name" and "Product Description".
And my code is
SELECT name AS 'Product Name' , description AS ' Product Description' FROM products;
But is says an error..
Can you please tell me where i am doing it wrong? thanks
The columns you selected were named Product Name, Product Description and not 'Product Name' and 'Product Description'.
As the exercise you posted says, the column alias names must be without '. Since the aliases have spaces in them, you would need to enclose the names with [], not with '.
Try this:
SELECT name AS [Product Name], description AS [Product Description] FROM products;
It seems the task 1 is waiting for double quotes but you're using single quotes.
Anyway, you can use single quotes, it's correct.
Backticks work well for Mysql. Try this
SELECT name AS `Product Name` , description AS `Product Description` FROM products;
SQL identifiers must be in backticks when using MySQL. You put single quotes around the column names.
And by the way: You have an extra space in ' Product Desciption'
This is a question for learning sense more than I need to find a solution.
I have a product entity, and it has an attribute of product_name. I would like to search product_name and return all products that have more than 1 'a' in the product_name. I need help with the query for this.
This code shows all products that have 'a'
SELECT product_name AS 'Product Names' FROM product WHERE name LIKE '%a%'
BUT I'm after products that have more than 1 'a'.
Structure: Product
Product(p_Id, product_name, p_cost)
So if I had a product called "Car Manual" it would return as there are 3 'a's.
Please help.
The query would look like:
SELECT product_name AS ProductNames
FROM product
WHERE name LIKE '%a%a%';
Don't use single quotes for column aliases. The best thing to do is to name the columns so they don't need to be escaped. Otherwise use backticks, double quotes, or square braces, depending on your database.
Another trick you could use is to remove all 'a' characters in the name column and then check to see if the length has decreased by two or more:
SELECT product_name AS ProductNames
FROM product
WHERE LENGTH(REPLACE(name, 'a', '')) <= LENGTH(name) - 2
I am trying to get data from my database. I have no problems with this, the SELECT statement works fine, but I want to select the record with a symbor or text before the result.
When I run the code:
SELECT price
FROM products
WHERE id = "1"
Ill get 5.00. But in this case I want to select it with a "€" symbol before the price. I found some scripts on this site where they are using the following code:
SELECT "€ " + price as 'price'
FROM products
WHERE id="1"
When I run this i get the same result as in the first code. So my question is: How can I select the price from the database with a symbol or text before (or after) the result?
Since you specified what is your RDBMs your answer is:
SELECT CONCAT('€ ', price) as price
FROM products
WHERE id=1
Don't use double quotes to use strings, double quotes on SQL is to name things like columns as alias and only use it when you want a field named with characters that the database wouldn't allow it like "Column name with spaces"
Also, ID is probably a number, so no need to use quotes since it will imply in implicit conversion which will make your query slower.
I'm working with the InnoDB version of MySQL's world database (available here) and am trying to fetch a list of the countries of South America and their capitals. In my mind, the query should look like this:
SELECT `Country.Name` as `CountryName`, `City.Name` as `CityName`
FROM `Country`, `City`
WHERE `Continent` = 'South America' AND `ID` = `Capital`;
But that one gives error #1054 - Unknown column 'Country.Name' in 'field list', even though the table Country does have the field Name.
Why isn't MySQL finding the fields I want? How do I change the query to make it find them?
Let me know if I need to provide more information for you to be able to help me.
If you quote identifiers, do not surround the inner dot with backticks.
SELECT
`Country`.`Name` AS CountryName,
`City`.`Name` AS CityName
If you quote around the inner dot, it will be assumed to be inside the column name, rather than a separator between the table name and column name -- you have a column named Name, but not a column called Country.Name. In this case, however, it is unnecessary to quote any of the identifiers since none of them are MySQL reserved keywords.
Try modifying your back ticks.
SELECT
`Country`.`Name` as `CountryName`,
`City`.`Name` as `CityName`
FROM `Country`, `City`
WHERE `Continent` = 'South America' AND `ID` = 'Capital';
Do not put them around the entire table.column, but around them individually with the period between them.
Also capitol should be single quotes and not back ticks.
This appears to be a syntax issue:
Try using this instead:
`Country`.`Name`
country.name (in ticks) would be the name of the field when what your after is
`country`.`name`