add column during mysql select - mysql

I'd like to add a column to the results of a MySql select statement. For instance, if I have a table:
|id||name||last_name|
Then when I do a select I'd like it to return:
|id||name||last_name||some_value|
where I specify |some_value| at query time. Is this possible?

Try this
SELECT
id, name,last_name, 1 AS some_value
FROM table

Yes, that is possible.
If you want to just hard code a value you can do this:
SELECT id, name, last_name, 'value' AS some_value
FROM table
Or you can aggregate or modify existing data in the table like this:
SELECT id, name, last_name, CONCAT(name, ' ', lastname) AS some_value
FROM table

Related

Mysql show other different cell data with under group by same name

Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)
One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*

mysql use WHERE and LIKE with diffrent columns

I can't find an answer for such a situation:
I have a table:
id, name, state
I want to search NAME column but only for records WHERE state=1
so something like this (of course it's wrong but the idea)
SELECT id, name FROM table_name WHERE state=1 AND WHERE name LIKE '%ss%' ORDER BY name
You can use only one where with and/or operations.
WHERE [condition] AND [condition] OR [condition] AND [condition]
You can use this following code
SELECT id, name FROM table_name WHERE state=1 AND name LIKE '%ss%' ORDER BY name

mysql find out the non repeated customers

I have a table like this,
test#text.com
test12#text.com
test123#text.com
test12#text.com
test#text.com
test12#text.com
test1#text.com
I want the email which are not repeated such as result will be
test1#text.com
Assuming your table name is sample_table your column name is sample_column.
Try this
select sample_column
from sample_table
group by sample_column
having count(sample_column)=1;
DEMO
You can try this...
SELECT DISTINCT(email_column_name)
from tableName
GROUP BY email_column_name;
and if you want more columns information related row then try this.
SELECT DISTINCT(email_column_name), ID, firstName,LastName
from tableName
GROUP BY email_column_name;
This is incorrect:
SELECT DISTINCT(email_column_name), ID, firstName, LastName
DISTINCT works on all columns selected, it is not a function where you can apply it to a single column in the select. So if an email were to appear twice with a different last name that email would be returned in both cases.

MYSQL explode search

I have a field that is build like this "1;2;3;4;8;9;11;"
If I want to search if a number is in this range I do it like this:
SELECT * FROM table WHERE [id] LIKE '[number];%' OR '%;[number];%'
Is there another more easy way where i can split the string?
Many thanks
If you are storing the values in a string, the best way to use like is as:
SELECT *
FROM table
WHERE concat(';', #numbers) like concat('%;', [id], ';%')
MySQL also offers find_in_set() when the delimiter is a comma:
SELECT *
FROM table
WHERE find_in_set(id, replace(#numers, ';', ',')
Use IN() with a comma delimited string of IDs
SELECT * FROM table WHERE id IN(1,2,3,4,8,9,11)
create another table called helper with 2 fields: number and id. you will need to programmatically create it by splitting the id field and add one row to this table for each number in each id.
create an index this table on number
select table.* from table where id in (select id from helper where number = 23)
you can use this
SELECT name ,`from`, find_in_set('4' , REPLACE(`from`, ';', ',')) as the_place_of_myval FROM reservation
having the_place_of_myval != 0
note: that in my query im searching for strings which have number 4
demo here in my demo there is two examples.

Masking values in SQL select return

This is an odd question, but I was wonder if you could display one column in the results, but really have another column as the values (MYSQL). Suppose I have this table:
ID Name
1 Soccer
2 Football
I was wonder if it was possible to select all IDs from this table (select ID from table), but the results would display the name instead.
Or is it possible to display as the result (as a single column)?:
1 (Soccer)
2 (Football)
SELECT ID, CONCAT("(", name, ")") FROM <TABLENAME>
This will have results concatenated into single column.
SELECT CONCAT(ID, ' (', Name, ')')
FROM tableName
SQLFiddle Demo