How to record data derived from COUNT DISTINCT query? - php

how to record data (into the database) derived from COUNT DISTINCT query?
For example,
Table1
AA
BB
CC
BB
BB
Then the COUNT DISTINCT query outputs the following data 131 successively based on table1.
How to automatically create a table for 131 output?

You seem to be looking for MySQL's CREATE TABLE ... SELECT Statement:
create table mydistincttable as
select ... -- your actual query goes here
MySQL creates a new table with one column for each column returned by your query, (with equivalent datatypes) and fills it with the results of the query. The names of the columns of the created table follow those returned by the query.
As you would expect, that the created table has no indexes, constraints, or the-like - because such information cannot be inferred from a query. If you want such features, you need to add them manually after the table is created.

that can be done with mysql VIEW
CREATE VIEW view_name AS your_query ;

Related

MySQL migrate data from a table with a set field

I have a table with a set column that contains 2 languages and another column on this table containing an order.
Is it possible migrate my table and to create a record in a new table for each set item with a MySQL query?
Directly migrating records works with records that have a set column without a language combination. But if a record would contain a set of languages I am left with one record with an empty column for language. I would preferably want 2 new records inserted into my fresh table.
One for each language in the set.
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, oldtable.languages, oldtable.order
FROM oldtable
WHERE oldtable.order IS NOT NULL;
I doubt it is possible but I would still like to know if somebody might have a workaround for this since I need to migrate the data somehow.
Create a helper table with one field that lists all languages that can be in the set column. The table can be a temporary one that you drop after the migration. Then create an insert ... select ... statement in which you join the source table on the helper table using MySQL's find_in_set() function:
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, helpertable.language, oldtable.order
FROM oldtable inner join helpertable ON find_in_set(helpertable.language, oldtable.languages)>0;

Uniquely identify same column name of two different table after join - mysql

I have 2 tables. suppose a & b
a has id, name, roll. b has id,group,name
This name column data are not same. How can I select and uniquely identify them?
I know about
SELECT a.id,a.name,a.group FROM a,b ............
I know this. But this is an example. I am working with huge amount of data with 20-30 columns in each table. So I don't want to write the column names I need to select rather I want to write the names that I want to exclude.
Like
SELECT * Except b.name............
OR is there any way to uniquely identify after join. Like
.......... a,b WHERE a.name as name1
Please don't ask why those column names are same. I admit it was a mistake. But it's already implemented and heavily used. So finding another way. Is there any simple way to exclude a column while merging them?
Well, you can't write the names you wish to exclude. That is not how SQL works.
However, if writing out 20-30 column names is that much of a burden, you can use information_schema.columns. I write it that way, because 20-30 column names is not particularly large and writing them out is probably less effort than writing the question.
But, back to the solution. It looks something like this:
select concat(c.column_name, ' as ', 'a_', column_name, ', ')
from information_schema.columns c
where table_name = 'a' ;
You might want to include the table schema as well.
As an IDEA, what you can do is, if you want to avoid columns of specific table & your statements have multiple table, you can try following,
Suppose you have 20 columns in table a & 5 columns in table b, you want to avoid col2,col3 & col4 of table b. Standard method is that you should write name of all columns of table a & required columns of table b. But you can avoid to write long list of 20 columns of table by writing a.* & then type required columns of table b. Please see below statement.
Select a.*,b.col1,b.col4,b.col5 from a,b
But if you require to exclude some columns from both table, then I think there is no other way than writing all required column names from both table.
There is no way to exclude a column in SQL SELECT Statement, you can only select a column. You can give alias name to columns while selecting them like below, so that you can identity columns using those alias names.
SELECT a.id as [column1],a.name as [column2],a.group as [column3] FROM a,b ............
There is no way to exclude a specific column but you can avoid to write all columns name and easy your job by below steps-
Step1: Execute below query-
SELECT a.*,b.* FROM a,b ............limit 1;
Step2: Export it into csv format with headings.
Step3: Copyp first (heading) row from csv.
Step4: Delete columns, those are not required and use other columns in your query.
There's only one waY i could see-
first create a temorary table
CREATE TEMPORARY TABLE IF NOT EXISTS mytable
(id int(11) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM;
then put your column in temporary table-
SELECT * INTO mytable
FROM YourTable
/* Drop the cloumns that are not needed */
ALTER TABLE mytable
DROP COLUMN ColumnToDrop
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable

Set column to automatically pull data from referenced table

I have one column called 'speeding' in a table containing many other columns. This column contains an integer that is foreign key to an entry on a table named Speeding. This table has columns 1-25 and an id that is referenced by the 'speeding' column from the first table.
Besides using join, is there any setting I can set on 'speeding' to make it automatically pull the associated data from the table Speeding?
You could create a view, a view is basically a SQL statement that is stored on the MySQL server and acts like a table
CREATE VIEW ViewName AS
SELECT tbl1.data, tbl2.speeding
FROM tbl1
INNER JOIN tbl2 ON tbl2.key = tbl1.key;
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
You then use the view as you would use any table
SELECT data, speeding
FROM ViewName

creating a mysql table from a inner join

I'm trying to create a mysql table from the inner join between two other tables. I'm dealing with a database someone creates which has the following tables:
sitematrix_sites
sitematrix_databases
They are related by another table (I don't know why don't use a foreign key) called sitematrix_sites_databases which has the following fields:
site_id and database_id.
That's how the two tables relate. Now I'm trying to remove that to make my life easier, so I have:
mysql> CREATE TABLE result AS(select * from sitematrix_databases INNER JOIN site
matrix_site_databases ON sitematrix_site_databases.database_id = sitematrix_data
bases.database_id);
ERROR 1060 (42S21): Duplicate column name 'database_id'
However, I'm getting that error. Does someone know how can I merge the two tables without repeating the database_id field?
Thanks
Remove the * in your SELECT statement and actually list out the columns you want in your new table. For columns that appear in both original tables, name the table as well (e.g. sitematrix_databases.database_id).
Don't use * instead name each column and use aliases. For instance instead of sitematrix_database.database_id you can have alternativeName. Also you can pick and choose which columns you want this way as well.
In SQL Server, you can use "select into". This might be equivalent syntax for mySql:
http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
Unfortunately, it's a two commands (not just one):
http://www.tech-recipes.com/rx/1487/copy-an-existing-mysql-table-to-a-new-table/
CREATE TABLE recipes_new LIKE production.recipes; INSERT recipes_new SELECT * FROM production.recipes;
Instead of using SELECT * ... try SELECT database_id ...
MySQL does not like joining tables that have the same column name.

How do I find which of two tables has the row with a given value in a known column

If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;

Categories