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
Related
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*
I have a mysql table where I some data are repeated in one column but have different value in another. I want to concat them an them and create new string.
I am getting data in following format:
See rows with checkbox option and vodaphoneissues and comments are repeated twice, yet have different values in another column.
I want to concatenate that value with comma separated format.
There is a function called GROUP_CONCAT.
I do not know names of your columns, let's say they are COLUMN1, COLUMN2, etc. The code will be:
SELECT COLUMN3, GROUP_CONCAT(COLUMN4) FROM your_table GROUP BY COLUMN3;
Use GROUP_CONCAT for that purposes
SELECT id, GROUP_CONCAT(DISTINCT column_name ORDER BY column_name SEPARATOR ', ')
FROM table
GROUP BY comments, versions;
Also pay attention to GROUP BY clouse as it groups by few columns:
GROUP BY comments, versions;
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.
I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.
I want to know if there is any option to output multiple rows into a single row.
for example the regular select * from tbl_name will give a list of all records available in the table.
firstname lastname
---------- ------------
Lepanto Fernando
Lourdes Brillianto
Gerald Siluvai
Preferred output
firstname will have -> Lepanto###Lourdes###Gerald
lastname will have -> Fernando###Brillianto###Siluvai
Can we have some concatenation done to achieve the above.
Use GROUP_CONCAT()
select group_concat(firstname separator '###') as firstnames,
group_concat(lastname separator '###') as lastnames
from your_table
Use:
select GROUP_CONCAT(firstname SEPARATOR "###") as firstname,
GROUP_CONCAT(lastname SEPARATOR "###") as lastname
from tblname