Concatenation doesn't work in phpmyadmin XAMPP - mysql

I tried to use "||" for concatenation:
The query I used:
"SELECT id, name, age, age||id FROM myTable;"
This is the output:
Can anyone tell me why the output is not 201 (in first column) and 162(in second column)?? Also it gives similar outputs when I use two attributes that are of varchar datatype and the above two attributes are of int datatype.

Can anyone tell me why the output is not 201
its because, in mysql, you need to enable PIPES_AS_CONCAT. in order to work with ||
If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the
SQL-standard string concatenation operator (like CONCAT()).
You can set it using phpmyadmin->variables->searchFor SQL_MODE
Refer mysql doc
But i would suggest you to use
CONCAT(columnName1, columnName2, ...)

There is no such concatenation in mySQL. This is Oracle SQL dialect. You have to use the CONCAT function in mysql
SELECT id, name, age, CONCAT(age,id) FROM myTable

visit the Mysql Documentations :
on this link you will find a list of String Functions and Operators
but you not use|| to concatenate caratcters or strings but do this :
SELECT id, name, age, concat(age,id) FROM myTable; or
SELECT id, name, age, concat_ws(' ',age,id) FROM myTable; if you want to space the age with id like this for example 23 1. 23 for age and 1 for id.

Related

Provide prepared statement in rails

I am using rails-4.2.1 and is trying to fetch data from two tables subjects and elective_subjects table in a single query. As rails 4 does not support UNION , I wrote a raw sql query. I want to search by name in both tables. My code is given below
query = "(SELECT id as id, name as name, reference as reference from subjects where name like '#{search}') UNION (SELECT id as id, name as name, null as reference from elective_subjects where name like '#{search}')"
#subjects = ActiveRecord::Base.connection.execute(query)
It is working but when I provide ' in my search the query breaks. So how can I make it a prepared statement. So that sql injection can be avoided
This question is super old and no cares anymore, but I think it is a valid question, so here's the answer:
query = "(SELECT id as id, name as name, reference as reference from subjects where name like $1)
UNION
(SELECT id as id, name as name, null as reference from elective_subjects where name like $1)"
binds = [ActiveRecord::Relation::QueryAttribute.new('name', search, ActiveRecord::Type::Text.new)]
result = ApplicationRecord.connection.exec_query(query, 'SQL', binds, prepare: true)
#subjects = result.rows
That's how you create and use a prepared statement in rails.
I have solved the issue by escaping the search string using following statement.
search = Mysql2::Client.escape(search)

SQL Using the OR & AND operators (Newbie)

I am using the OR operator in the following fashion:
SELECT * FROM Customers
WHERE City = "Berlin" OR City = "London"
The above returns a results table containing some data, however what also returns data is:
SELECT * FROM Customers
Where City = "Berlin" OR "London"
I am using a questionable SQL editor currently, and want to know if this would be valid in a MySQL environment.
You are looking for
SELECT * FROM Customers Where City IN('Berlin', 'London');
The query:
Where City='Berlin' OR 'London'
Applies the logical OR operator (||), so OR "London" is equivalent to OR 0, and Where City = 'Berlin' OR 0; will just return 'Berlin'
SqlFiddle here with truth table here
Minor, but you should look at using single quotes for string literals, as this is more portable between RDBMS's and use of " will depend on ANSI QUOTES.
Well, Cassini i have check your syntax and run this query on my table i did not getting same result. I am getting correct output as expected.
i have run this command:
SELECT * FROM `city` where name = 'London' or 'Berlin'
and i got only london in name column. When i run this command:
SELECT * FROM `city` where name = 'London' or name = 'Berlin'
then i got both the cities in name column. so command is valid it will return only valid output which satisfy the condition.
So, i can say that command is valid but execute only that part of query which satisfy MYSQL Select syntax.
Well, if it valid in SQL, it should be valid in MySQL to. Are you wondering if this would be valid in MYSQL
SELECT * FROM Customers Where City="Berlin" OR"London"
It should be valid in MySQL
Hope that helps. If it does not, add a comment
this returnd the same values as your first sql-string.
But your first string is more readable for others.
You can use IN
SELECT *
FROM Customers
Where City IN ("Berlin","London")

LIKE '[charlist]%' syntax not working in MySQL (phpMyAdmin)

There is table named Students. I want to extract the names of students whose name starts with either 'n' or 'p' or 'y'. I know that in TSQL (MS SQL server) I can write the query as follows and it works:
SELECT * FROM Students WHERE StudentName LIKE '[npy]%'
But when I execute the same query in MySQL (phpmyadmin) I was unable to retrieve the correct result set. I tried converting the letters of the student name into the same case which is mentioned in the charlist. I did bit of googling and found out that in MySQL we need to specify this as a regular expression. I executed the below query and got the expected result.
SELECT * FROM Students WHERE StudentName REGEXP '[[:<:]]n | [[:<:]]p | [[:<:]]y'
I want to know the reason why LIKE '[charlist]%' syntax is not working with MySQL. Is it due to the implementation in MySQL (MySQL doesn't support the syntax) or something wrong with the phpmyadmin version I'm using?
Any help regarding this is highly appreciated. Thanks :)
There is an even shorter way to write your query:
SELECT * FROM Students WHERE StudentName REGEXP '^[npy]'
And if you're concerned about being case sensitive:
SELECT * FROM Students WHERE StudentName REGEXP BINARY '^[npy]'
In MySQL, a REGEXP pattern match succeeds anywhere in the value, which differs from LIKE where the pattern must match the entire value.
The following link will give you a more complete answer:
MySQL Pattern Matching
MySQL :
Case insensitive: SELECT * FROM Students WHERE StudentName RLIKE '^[npy]' ;
Case sensitive : SELECT * FROM Students WHERE StudentName CAST(RLIKE as BINARY) '^[npy]' ;

can mysql concat only take two arguments

I am trying to build a string using MySQL 5.5.15 but it seems to not work for more than two args:
mysql> select concat(id, name) as me from locations; # this works
mysql> select concat(id, name, website) as me from locations; # doesn't work
Some of the examples have > 2 args but it just doesn't seem to work. Should it work?
try using CONCAT_WS()
SELECT CONCAT_WS('', id, name, website) AS me FROM locations
-- ^ this is an empty char separator,
-- you can define what ever you want
MySQL CONCAT_WS()
This may be a wild guess but I think the value of column website is NULL. CONCAT may act differntly with CONCAT_WS() because it doesn't convert NULL values into default string.
Here's a simple DEMO: http://www.sqlfiddle.com/#!2/c8d79/3

Need MySQL idiom for checking whether string contains any of a set of characters

I need to run the following MySQL query:
SELECT id, name FROM mytable
WHERE NOT CONTAINS_ANY(name, ';.<>#$!');
except that there doesn't seem to be anything like CONTAINS_ANY (taking two string and checking whether the first string contains any character of those in the second string). What can I do instead? I would like to avoid
SELECT id, name FROM mytable
WHERE name NOT LIKE '%;%'
AND name NOT LIKE '%.%'
AND etc. etc.
and similar ugliness.
Use regex:
SELECT id, name
FROM mytable
WHERE name not rlike '[;.<>#$!]';