Select query with custom column and value of other column - mysql

I want to have a query that have a custom column and have some value of other column
Here is example of a table
Table 1 : id, name, emp_id
I want to have input like this
id | name | emp_id | process |
1 | John | 002 | Work of John |
The process is just custom column I want to do it in Select query. Laravel eloquent query much more appreciated, since I'm doing it in laravel.
This is what I tried:
Select id, name, emp_id, "Work of " + name as process,
from table1
But it's not working. I also want to add 2 or more column value in custom column.
Thanks

I think you are looking for concat():
select id, name, emp_id, concat('Work of ', name) as process
from table1

Related

How to copy data from table to another table with the same column only

I want to copy datas from the first table to another table. Both tables have common columns.
This is the representation
table1 has already data.
table 1:
id | employeeName | sectionCode | teamCode| day1 | day2 |
1 eric 400 315
and table2 has no data yet.
table 2:
id | employeeName | sectionCode | teamCode | day1_a | day_b |
i want to copy the data inside table 1 to table 2 but only the common columns. So that i don't need to enter data in table 2 manually.
i tried this question but it's different.
Explicity name the "common" columns in the target table and select only those:
insert into table2 (id, employeeName, sectionCode, teamCode)
select id, employeeName, sectionCode, teamCode
from table1
This assumes that the other columns are defined with default value or are nullable. If not, or alternatively, you can omit naming the target columns if you provide values (or expressions) for the other columns so all columns have values provided for them:
insert into table2
select id, employeeName, sectionCode, teamCode, 'foo', 'bar'
from table1

Remove SQL duplicates

I'm totally new in SQL. I never used it and just need a simple answer because I don't have time to learn SQL right now :(. I need to remove duplicated records from my local DB. Case looks like this:
| id | type | ... |
-------------------
| 1 | test | ... |
| 1 | test2 | ... |
| 1 | test | ... |
| 1 | test | ... |
I want to remove all duplicated record which has the same id and type but leave only on record. Like this:
| id | type | ... |
-------------------
| 1 | test | ... |
| 1 | test2 | ... |
Using delete by Id is impossible. I have 50k records and I want to remove all duplicated records. When ID and Type are the same.
Please try this
First Way
SELECT id, type
FROM table_name
Group by id, type
Second Way
SELECT DISTINCT id, type
FROM table_name;
A TSQL sample code that might help:
WITH tbl_alias AS
(
SELECT id, type,
RN = ROW_NUMBER()OVER(PARTITION BY id, type ORDER BY id)
FROM tbl
)
DELETE FROM tbl_alias WHERE RN > 1
Also you can try How to delete duplicates on a MySQL table?
.
SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
SELECT DISTINCT column1, column2, ...
FROM table_name;
In your table
SELECT DISTINCT id, type, ...
FROM table_name;
you just need to use the keyword distinct when selecting mate.. try like this
SELECT DISTINCT id, type, blah blah blah FROM your_table; // this should take care of em
You should replace your table grouping by id and type, and using an aggregate function on the other fields.
You should add to your question the definition of your table and specify the rule to use to get the other fields. Anyway, this is a simple solution:
-- Create a temp copy of original table with distinct values
CREATE TEMPORARY TABLE copy_table1
SELECT id, type, MIN(field3) AS field3, ...
FROM table1
GROUP BY id, type;
-- Empty original table
DELETE FROM table1;
-- Insert distinct data into original table
INSERT INTO table1 (id, type, field3, ...)
SELECT id, type, field3, ...
FROM copy_table1;

MYSQL Echo Value only once from multible columns

I would like print out a list of names, each name only once.
The Problem: I have two different columns with names in them, yet I want to display each name only once, not depending on the column.
I am going to echo the result with PHP, so also loop's or other PHP actions are possible if there is no MYSQL only solution.
To visualise an example: (I also have an ID column as shown below)
ID |Name1 | Name2
-------|-------|-------
01 | A | B
02 | A | C
03 | B | A
As seen above there are the same names in both columns, yet I want to get each name only once.
Locking like this (for example):
(List:)
A
B
C
something like this should do it:
select distinct name
from (
select Name1 name from table_name
union all
select Name2 name from table_name
)
try this:
select distinct(name) from (select a.name1 as name from tablename as a
union select b.name2 as name from tablename as b)name;

MySQL : interval around id column and return another one from subquery with multiple columns

I would like to run a query from a table where the content is like that :
id | col1 | col2 | col3
-----------------------
1 | i_11 | i_12 | i_13
2 | i_21 | i_22 | i_23
3 | i_31 | i_32 | i_33
.. | ... | ... | ...
SELECT col1 FROM table WHERE id IN
(SELECT id-1, id+1 FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz')
The aim is to get an interval [id-1, id+1] based on the id column which returns the content stored in col1 for id-1 and id+1. The subquery works but I guess I have a problem with the query itself, since I'm having an error "Operand should contain only one column". I understand it, but I don't see any other way to do it in one query ?
I'm quite sure there's a pretty easy solution but I can't figure it out for the moment, even after having carefully read other posts about multiples columns' subqueries...
Thank you for any help :-)
The only way I can think to do it right now is like this:
SELECT col1
FROM table T
WHERE id BETWEEN (SELECT id FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz') -1
and (SELECT id FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz') +1
Your problem is that you are retrieving two values - but as a list rather than a set. The SQL optimizer can't see 1,3 as a set of two items when they are presented in a single row. There may also be a cast needed.
This should work.
SELECT col1 FROM table WHERE id in
(
select cast(id as int) -1 from table where col1='i_21'
union
select cast(id as int) +1 from table where col1='i_21'
)

Reorder rows in a MySQL table

I have a table:
+--------+-------------------+-----------+
| ID | Name | Order |
+--------+-------------------+-----------+
| 1 | John | 1 |
| 2 | Mike | 3 |
| 3 | Daniel | 4 |
| 4 | Lisa | 2 |
| 5 | Joe | 5 |
+--------+-------------------+-----------+
The order can be changed by admin hence the order column. On the admin side I have a form with a select box Insert After: to entries to the database. What query should I use to order+1 after the inserted column.
I want to do this in a such way that keeps server load to a minimum because this table has 1200 rows at present. Is this the correct way to save an order of the table or is there a better way?
Any help appreciated
EDIT:
Here's what I want to do, thanks to itsmatt:
want to reorder row number 1 to be after row 1100, you plan to leave 2-1100 the same and then modify 1 to be 1101 and increment 1101-1200
You need to do this in two steps:
UPDATE MyTable
SET `Order` = `Order` + 1
WHERE `Order` > (SELECT `Order`
FROM MyTable
WHERE ID = <insert-after-id>);
...which will shift the order number of every row further down the list than the person you're inserting after.
Then:
INSERT INTO MyTable (Name, `Order`)
VALUES (Name, (SELECT `Order` + 1 FROM MyTable WHERE ID = <insert-after-id>));
To insert the new row (assuming ID is auto increment), with an order number of one more than the person you're inserting after.
Just add the new row in any normal way and let a later SELECT use ORDER BY to sort. 1200 rows is infinitesimally small by MySQL standards. You really don't have to (and don't want to) keep the physical table sorted. Instead, use keys and indexes to access the table in a way that will give you what you want.
you can
insert into tablename (name, `order`)
values( 'name', select `order`+1 from tablename where name='name')
you can also you id=id_val in your inner select.
Hopefully this is what you're after, the question isn't altogether clear.