Mysql not working when used double alias - mysql

Hey guys am really new to mysql.I have heard of alias in mysql and i have tried double alias with the string like
select ('name' as bae,'age' as ages) as person;
When i run the above code it doesnt give me the output and raises the error.I dont understand why the double alias didnt works in mysql.
Any help to make this one correct would be really appreciated..Thanx in advance

The problem with your query are the parentheses. They represent a single expression and you don't put aliases (or commas usually) inside expressions. So, this fixes the immediate problem:
select 'name' as bae, 'age' as ages
I'm not sure what the second "as person" is supposed to be. Perhaps you want a subquery:
select person.*
from (select 'name' as bae, 'age' as ages) person

A really basic MySQL query should look something like:
SELECT
[COLUMN_NAME] AS [ALIAS_NAME]
FROM
[TABLE_NAME]
Based on your query it appears you are trying to retrieve the two columnns name and age from the person table.
If this is the case, the following query can help you:
SELECT
name AS bae,
age AS ages
FROM
person
But, this is still just one big guess, if you really want us to help you with your problems, you should give some more information about what you are trying to achieve.

Related

mysql min and max values in subquery

thanks in advance for the help. I'm a noob with sql and this is probably a pretty basic question, but I have been working on it for hours and can't seem to figure out what I'm doing wrong.
Here is the question posed:
Using aggregate functions, display the employee name (first and last name concatenated) in a field named Employee Name and the hire date of the employee who has been employed the longest and the employee who was hired last.
I have tried to code this different ways to no avail. I always get one result instead of the two I know I should get. (I know I should get two results because I'm working with a very small table and can calculate the answer by looking at it - easy to make sure I'm getting the right results that way.)
Here is one way I coded it:
subquery
Here, I used a sub query for the min and max. I used 'and' in between because I want both results. I only get one. If I use 'or', I get too many results
I have also tried it this way:
min/max in select statement
Here, I still get only one employee name back, but now I get both dates. The dates are the correct dates, but I need the output to look like the first query output.
Any help anyone can provide would be greatly appreciated!
How about
select concat(first_name,' ',last_name), hire_date
from l_employees
where hire_date = (select max(hire_date) from l_employees)
or hire_date = (select min(hire_date) from l_employees)

Remove variable part of string in Mysql Workbench

Halo all.
I have a table with two columns. One of them with customer_id and another one with acceptence_reason. I want to delete the customer_id from the acceptance part but have not yet come up with a effective way to do so.
To avoid confusion I have added a picture to clearify my problem
I suppose you already tried substr() but maybe you didn't know instr(). So I would try that:
select Customer_id, substr(acceptance_reason, 0, instr(acceptance_reason,"(") ) as acceptance_reason from ...

MySQL subquery quirk or expected behaviour?

I'm trying to gain more knowledge concerning the MySQL database and I'm hoping someone over here might be able to explain to me the following issue as I cant find much about this particular behavior anywhere:
This works:
SELECT justaname FROM (SELECT productName AS justaname FROM kclbs_products) sdfsdfsd
While this doesnt:
SELECT justaname FROM (SELECT productName AS justaname FROM kclbs_products)
This really puzzle's me and I believe it to be a quirk because whatever I turn the string 'sdfsdfsd' into doesn't matter, the query still works, even when its just a single character (or a very large sequence of characters for that matter). This 'issue' doesn't represent a problem to me currently but I would really like to know the 'why' behind it to be able to deal with this kinda behavior anywhere in the possible future should I ever have to.
Thanks in advance.
[UPDATE]
Two users have helped providing me with the answer to my question, so its solved, and thanks!
It subquery name
SELECT ... FROM (subquery) [AS] name ...
Here is reference from mysql docs
The [AS] name clause is mandatory, because every table in a FROM clause must have a name. Any columns in the subquery select list must have unique names.
For futher info use https://dev.mysql.com/doc/refman/5.7/en/from-clause-subqueries.html
This is not related to MySQL, and its a rule in all DBMS's. if you use subquery in FROM clause you must alias it.
When you write something after subquery (like sdfsdfsd), DBMS consider it as alias name of table derived from subquery.
You can see some information about subqueries in FROM clause in:
https://dev.mysql.com/doc/refman/5.7/en/from-clause-subqueries.html
and
http://www.geeksengine.com/database/subquery/subquery-in-from-clause.php
it also explained in subquery in FROM must have an alias

Why is my count function only working on certain tables?

I know this is an amateur question, but I've searched every resource I can think of, and now I'm at my wit's end. The following query works perfectly on most of my tables, but for some reason it is not working on the tables that I desperately need it for:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE daypart LIKE 'AM';
The query works exactly how I want it to for nearly all of my tables, but for some reason it is returning a value of "0" on the tables I need it for (even though there are over 20 instances of "AM" in the "daypart" column on this table). I have checked and double-checked everything I can think of.
I'm relatively new to SQL but I've never encountered a problem like this before. Anyone have any ideas or resources that might help? Thanks so much for your time!
EDIT: I don't have enough reputation points to post a screen shot on here... but here's a link where you can see one: http://imgur.com/ZhyEqJY
There are 29 columns in this table. If there's any other info that might help just let me know, thanks!
You need to add the like part as shown below:
where column_name like '%AM%'
when you write like 'AM' it is searching for the full match
Try this.
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
If you want to order it using the count,
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%' ORDER BY COUNT(*) DESC;
DESC - Descending order
ASC - Ascending order
LIKE statements are really slow in sql. If you have a daypart column then I assume you have a date column. I recommend something like this instead:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE HOUR(dateColumn) < 13
Unless the string you're matching is exactly AM you need to use wildcard characters to match the string:
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
The % matches any number of characters before and after AM.
See the documentation for more information.

MySQL: Selecting most similar value?

So I have a table with peoples names.
I need to code a query that selects the person with the name most similar to the given one.
For example:
SELECT * FROM people WHERE name='joen'
'joen' doesnt exist in the table, so it will return John, which exists in the table.
What's the MySQL command for this?
You may be looking for SOUNDEX and SOUNDS_LIKE
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
The Levenshtein algorithm computes the "distance" between words: Levenshtein MySQL function
You can use it like this if you add that function:
SELECT * FROM people WHERE levenshtein('joen', `name`) BETWEEN 0 AND 4;
I'm not an expert on this, but this is no trivial matter. By "similar" do you mean it might be one or 2 letters off, or do you mean "Jon" should match "Jonathan" and "Bill" should match "William"?
If the latter, you might want to find or build a table that maps names/nicknames to eachother, and do a search on that.
If it's misspellings, Levenshtein might be of assistance, but I don't know how you'd integrate that in an SQL query.
The LIKE Keyword might help you
SELECT * FROM people WHERE name LIKE 'jo%'
Selects users where name starts with "jo"
If you're programatticaly changing the query, you
can check for the "result" and query the database again
with the new Query by reducing the characters in the
"name" specified.
SELECT * FROM people WHERE name LIKE 'jones%'
SELECT * FROM people WHERE name LIKE 'jone%'
SELECT * FROM people WHERE name LIKE 'jon%'
SELECT * FROM people WHERE name LIKE 'jo%'