SQL Derived Table - mysql

I am new to SQL and really struggle just testing it out. My question is how do I derive data from one table to a new one keeping only specific data like:
-Real name
-Screen name
and also how do I create new variables using SQL. For example the Number of tweets that the person contributed.

In MySQL, you would use create table as:
create table table2 as
select RealName, ScreenName
from table1;
However, you don't actually need to copy the data. You can just use a view instead:
create view table2 as
select RealName, ScreenName
from table1;
Or, just put the logic into your query.

SELECT ur specific column
INTO newtable
FROM oldtable;
check here for more examples

Related

Copy data from few tables using SQL script

So let's say I have a table Car and it has primary key ID, columns BrandID (ref to table Brand), Price, Comment and others.
.
The thing I need to do is to copy columns Price and Comment to the new table.
But also for every Car element I need to go to Brand table and get specific Brand Name depending on BrandID value and also copy it to the new table
How can I accomplish this via SQL script?
Create the new table direct from the SELECT statement with JOINed tbales
CREATE TABLE NEW_TABLE SELECT Price, Comment, name FROM car c INNER JOIN brand ON b.ID = c.BrandID
You can use
CREATE TABLE ... SELECT statement...
Take a look at this mysql document for details
https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html

Mysql: Put output of select as new column in table

I have mysql table
surname | name | complete
Now i want to connect "surname" and "name" with a SELECT CONCAT and put the output into the "complete" column.
Any Idea?
If you really want to do this the answer by #scaisEdge shows you how. But you really shouldn't! To do so would mean to introduce redunancy. That means your database wouldn't properly normalized. As a rule, you do not store data that is the result of a simple operation on one or more columns.
Consider using a VIEW instead.
CREATE VIEW extended_table AS SELECT surname, name, CONCAT(name,surname) AS complete
This does not have any storage overhead. Alternatively you can use a generated column.
ALTER TABLE users add column complete as CONCAT(name, surname);
Both these options allow you to maintain your table in it's normalized form and you are not saving any redundant data to disk.
#jarlh has raised a very valid point in the comments, how to deal with null
CREATE VIEW extended_table AS SELECT surname, name, CONCAT(COALESCE(name,''),COALESCE(surname,'')) AS complete
You can use update
update my_table
set complete = concat(surname, name)

Get all values from table and reuse them to create rows in other table

I have a table (People) with columns (id, name, year). Is there any way I can get all the ids ( SELECT id FROM people ) and use them for creating new rows in other table (people_id, additional_info). I mean that for every id in table People I want to add new row in other table containing the id and some additional information. Maybe something like for-loop is needed here ?
Well, usually you have information on a person and write one record with this information. It makes litte sense to write empty records for all persons. Moreover, when it's just one record per person, why not simply add an information column to the people table?
Anyway, you can create mock records thus:
insert into people_info (people_id, additional_info)
select id, null from people;
Insert into targetTable (id, additional_info)
select id,'additionalinfo' from PEOPLE
No for loop is needed, Just use the above query.
You can use INSERT ... SELECT syntax for MySQL to insert the rows into people_info table (documentation here).
e.g.
insert into people_info(...)
select (...) from people; <and posibly other tables>

How to I functionally access an SQL table's table name?

I have a set of tables that I am conflating together. I want to be able to go back and access the 'raw' data later if I need to add more. The way I'm doing this by adding a reference column to the conflation table that will contain the table name of the table from which the data came from.
How can I access the names of my tables as I query from them?
EDIT: Details:
The table I'm creating looks like:
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name
thing1.shape
FROM
public.thing1_source_table
UNION
SELECT
thing2.name
thing2.shape
FROM
public.thing2_source_table);
And I want to add the "source" field:
ALTER TABLE combined_things ADD COLUMN source_id character varying(100);
ALTER TABLE comnined_things SET COLUMN source_id = {table_name}
And I don't know how to pull the {table_name}
You could add them as a string constant when you create the table
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name,
thing1.shape,
CAST('public.thing1_source_table' AS CHAR(100)) source_id
FROM
public.thing1_source_table
UNION
SELECT
thing2.name,
thing2.shape,
'public.thing2_source_table'
FROM
public.thing2_source_table);
Note that there is no way of casting the column to varchar

Importing sql file data omitting columns (MySql)

I am building a new booking system in PHP at the moment and I want to take over most of the data of the old system (MySQL) however the database structure of the new system will be slightly different. However I think I could take over some of the tables. Is it possible to import rows of old tables to the new table given that the column names are equal but some column names in the new table might be missing?
an example, the old table:
tourguides
id name address email telephone_mobile telephone_home
the new table
tourguides
id name address telephone_mobile
the new table doesn't has the column telephone_home, this shouldnt stop the import but instead it should be ignored
Yes, it's possible.
You should do an INSERT SELECT with fixed fields, like this:
INSERT INTO table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM table2
You can even do it between databases:
INSERT INTO db1.table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM db2.table2