SQL SELECT statement with symbol or text - mysql

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.

Related

Issue revtrieving 100 elements using the lower() function

I'm having issue retrieving info from a DB by using this query with the lower() function:
SELECT DISTINCT "column_name" FROM "table" WHERE lower('car', 'house', 'plane'...) like '%owner%'
The query works with 1 attribute like for exmaple 'car' but when I try to use 100 I get the following error:
Error Code: 1582. Incorrect parameter count in the call to native function 'lower'
What should I change in order to be able to use and output more than a single attribute?
Thanks.
I think the query you want is something like this:
SELECT DISTINCT column_name
FROM yourTable
WHERE owner REGEXP 'car|house|plane'; -- and maybe other terms in the alternation
That is, you want to match all records where the owner column contains one of the substrings car, house, or plane.

SQL - How to search for a string that contains backslash

I'm trying to find all entries that contain a backslash anywhere, like so:
SELECT * FROM animals WHERE bodyType LIKE '%\\%'
I also tried:
SELECT * FROM animals WHERE bodyType LIKE '%\\\\%'
and
SELECT * FROM animals WHERE bodyType LIKE '%\\\\\\\\%'
Neither worked. Anyone know how to do this?
I am running the commands in MySQL Quick Admin v1.5.4
This question has not answer.
You try to match searching of content with backslash on names of columns.
Your (a bit general) query
SELECT * FROM tableName WHERE columnName LIKE '%\\%'
will give you any result if content of any column will contain backslash. In this case your query is correct. But you cannot match name of any column.
I looked into some books I have and all they say the same: this will select all records written in column of chosen name that are containing backslash. But columns have to be chosen exactly (they cannot be selected by name with using of SQL query).

How to search for substring in one of two fields of a table

I have a table with three columns: id, first, and last.
I'd like to find all records where 'SAM' is in either the first or last field.
I'm not an expert with MySQL, but it seems to me that one field could be queried using the LIKE operator.
How can I use LIKE in a query to get data from both columns at the same time?
I've tried this:
SELECT id
FROM `employees`
WHERE 'first' like 'SAM' OR 'last' like 'SAM'
But I get the message "You have an error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax."
I can't seem to figure out why you are getting a syntax error, but one problem with your query is that by putting single quotes around your column name causes it to be treated like a string literal instead of a column name.
What I mean by that is instead of comparing the value in the first column with the string 'SAM', it's comparing two strings, 'first' and 'SAM' which are different. This query would return no results.
In addition, this will only work if the first or last name is equal to sam. To check for those characters as a substring at any point, I would add wildcards at the front and back of the strings.
Try this:
SELECT id
FROM employees
WHERE first LIKE '%SAM%' OR last LIKE '%SAM%'
Here is an SQL Fiddle to show how it works.
I don't see any syntax error in the query you posted. What you have is really close.
Your query isn't comparing the contents of the columns; it's comparing string literals, because of the single quotes, those are strings, not column references.
The LIKE comparison is equivalent to an equality comparison, since the there aren't any wildcard characters in the string on the right side. The '%' character is a wildcard that will match any number of characters.
To return rows where either of the columns first or last contain the string "SAM", you could do something like this:
SELECT e.id FROM employees e WHERE e.first LIKE '%SAM%' OR e.last LIKE '%SAM%'
That query would match any of these example rows:
id first last
-- --------- -------
2 Flotsam
3 Samson
5 Sesame
7 Yosemite Sam

How do I select columns which has a dot in their column names

I am trying to select a record which clearly exists, but my SQL query does not bring it up. Any idea how to get this working?
SELECT * FROM Users WHERE 'local.email'='burgundy#email.com' LIMIT 1
The issue is that you're using single quotes ( ' ) around your column name, rather than using backticks ( ` ).
Try using this instead:
SELECT *
FROM Users
WHERE `local.email` = 'burgundy#email.com'
LIMIT 1
Like Crocodile said, anything that is a SQL variable like a table name or column name can also be surrounded by `` Back ticks (hold shift and hit ~). This tells SQL to look at them as literals.

Full JOIN MySQL Query is returning empty

So here is a MySQL Query:
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = 'LoggedCarts.Bill-Email'
LIMIT 0 , 30
It is returning an empty result set, when it should be returning four results based on the tables below.
First Table: LoggedCarts - Column: Bill-Email
casedilla#hotmail.com
crazyandy#theholeintheground.com
Second Table: TestSite - Column: email
samuel#lipsum.com
taco#flavoredkisses.com
honeybadger#dontcare.com
casedilla#hotmail.com
messingwith#sasquatch.com
The goal is to get a MySQL statement that returns the rows in Table: TestSite that don't match the rows in Table: LoggedCarts.
Note: I understand that the use of a hyphen in a column name requires special care when constructing a query, involving backticks to tell MySQL there are special characters. I would change the column names to match up, however the Table: LoggedCarts has data fed via post from a Yahoo Shopping Cart and without heavy preparation before insertion setting the name to anything but the key sent in the post data is daunting.
However, if it turns out rebuilding the data prior to insertion is easier than using a JOIN statement or for some reason using two columns with different names as the comparison columns just doesn't work, I will go through and rebuild the database and PHP code.
Single quotes indicate a string literal. You need to use backticks for identifiers. Also, each component of an identifier must be quoted individually.
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = LoggedCarts.`Bill-Email`
LIMIT 0 , 30
From the manual:
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`.
With a bit of research inspired by somne of the hints given, I found the solution I was looking for here: SELECT * WHERE NOT EXISTS
Does exactly what I need it to do, and as a bonus, I like the shorthand syntax that is used that allows you to put in an alias for the table name and use the alias throughout the statement.
SELECT *
FROM TestSite e
WHERE NOT EXISTS
(
SELECT null
FROM LoggedCarts d
WHERE d.`Bill-Email` = e.email
)