How can I create a view with duplicate columns and merge them? - mysql

I have pm_message tables with 1~9, and I want to create a view to simplified the process of MySQL query.
What I have is
CREATE VIEW `pm_messages` AS
SELECT * FROM
`pm_messages_0`,
`pm_messages_1`,
`pm_messages_2`,
`pm_messages_3`,
`pm_messages_4`,
`pm_messages_5`,
`pm_messages_6`,
`pm_messages_7`,
`pm_messages_8`,
`pm_messages_9`;
I got error with douplicate column. There is no record is duplicate, I want to merge all of them in view, what should I do?

You have coded a colossal cross join. Depending on the number of rows, it probably wouldn't return before the universe suffers entropy heat death.
I'm almost certain you want a union:
CREATE VIEW `pm_messages` AS
SELECT * FROM `pm_messages_0` union all
SELECT * FROM `pm_messages_1` union all
SELECT * FROM `pm_messages_2` union all
SELECT * FROM `pm_messages_3` union all
SELECT * FROM `pm_messages_4` union all
SELECT * FROM `pm_messages_5` union all
SELECT * FROM `pm_messages_6` union all
SELECT * FROM `pm_messages_7` union all
SELECT * FROM `pm_messages_8` union all
SELECT * FROM `pm_messages_9`;
This will work if all tables have the same number and type of columns. If not, you'll have to explicitly select columns such that each select returns the same number and type of columns.

Related

Php mysql bad query when displays on a table

there's my code that select 5 databases and display in a table, but the where statement not work
The where is ignored by the query.
SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive where atender='$obj'
I suspect that you want the WHERE criteria to apply to every subquery in the union. If you want that, you'll have to add a WHERE clause to each subquery. But, if you really do want to use a single WHERE clause, you can wrap your union query and then subquery it:
SELECT *
FROM
(
SELECT * FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive
) t
WHERE atender = '$obj';
Side note: Please use prepared statements in your PHP code wherever possible.
SELECT * FROM
( SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive)
AS derived
WHERE atender='$obj'

Mysql ORDER BY FIELD() in specific pattern

if anyone please help me , i am struck with it..
sorry for my bad english
My sql query is:-
SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
I want to do this id 1 should be first record, id 5 should be 6th record, id 4 should be 11th record and id 3 should be 16th record,
Is there a way to do this in mysql..
Please help me.
Your Code should work as long as you only want to view the IDs listed in the order by clause. Try to add the ID you are selecting to the where clause:
SELECT * FROM table
WHERE ID in(1,3,4,5)
ORDER BY FIELD(ID,1,5,4,3) ;
Could you test it? It is working fine for me.
You can also solve this problem with multiple selects and union.
SELECT * FROM table where ID=1
UNION ALL
SELECT * FROM table where ID=5
UNION ALL
SELECT * FROM table where ID=4
UNION ALL
SELECT * FROM table where ID=3 ;
UNION ALL will show a complete resultset. UNION without ALL will exclude duplicate results. In your case duplicates are impossible, so union without ALL would suffice :
SELECT * FROM table where ID=1
UNION
SELECT * FROM table where ID=5
UNION
SELECT * FROM table where ID=4
UNION
SELECT * FROM table where ID=3 ;

MySQL select from custom set and compare with table data

Hi I'm trying to solve which elements doesn't exists in my database. In order to do so I want to compare list of integers (output from external script) with data in table. How to do such thing like:
SELECT * FROM (1,1,2,3,5,8,13...) l WHERE l NOT IN (select id from table1);
This is probably best done with a left outer join. But, your problem is creating the table of constants:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids
where ids.id NOT IN (select id from table1);
This can have odd behavior, if table1.id is ever NULL. The following works more generally:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids left outer join
table1 t1
on ids.id = t1.id
where t1.id is null;
EDIT:
The size of a MySQL query is dictated by the parameter max_packet_size (see here). The most recent version has a limit of 1 Gbyte. You should be able to fit 18,000 rows of:
select <n> union all
into that limit, quite easily. Gosh, I don't even think it would be 1 megabyte. I would say, though, that passing a list of 18,000 ids through the application seems inefficient. It would be nice if one database could just pull the data from the other database, without going through the application.
If your set to compare is huge I'd recommend you to create a temporary table myids with the only column id, put there all your 18K values and run query like that:
select id from myids where myids.id not in (select id from table1);

mysql UNION result has less rows than sum of table row counts

I'm using UNION to get all names in different tables.
my tables has about 10000 rows all together.
but the query returns 468 rows!!
My query is:
SELECT name FROM `shopping`
UNION
SELECT name FROM stores
UNION
SELECT name FROM concert
UNION
SELECT val AS name FROM event
UNION
SELECT name FROM fastfood
Where is the problem?
UNION removes duplicate values. You probably want UNION ALL instead.

How to select sum across 100 tables?

I unfortunately have spread some data across about 100 tables, and I don't have time to reorganize it, so I am curious as to how I might do a select sum(column_name) across that many tables.
I have found examples of how to sum across 2 tables, but never an example of 100.
Anybody know how?
Addendum: Using a very large command has resulted in a "memory exhausted" error. Is there a way to do this using LIKE ?
I would say the easiest way to do this is to do lots of selects (one for each table) of the value you want to sum and just do a sum on the union of that?
Something like :
SELECT SUM(VALUE) FROM (
select 1 VALUE
UNION
select 2 VALUE
UNION
select 3 VALUE) as DATA
Of course the selects will be selecting a column from each table and not just an integer like this, but you get the idea ...
You seem fixated on LIKE, but LIKE is for WHERE clauses, not choosing tables to select from. If selecting from all the tables at once is too much for your system to handle, the obvious solution is to query each table individually and then add up the results using a perl/php/other script.
You can do something like this:
select sum(sumcol) FROM (
select sum(col) as sumcol from table1
union all
select sum(col) as sumcol from table2
union all
select sum(col) as sumcol from table3
...
union all
select sum(col) as sumcol from table100
);
This is done with much less memory consumption when you sum-up in union parts, too.
It would be a big query, but something like this would do it:
SELECT SUM(col) FROM (
SELECT col FROM table1
UNION ALL
SELECT col FROM table2
UNION ALL
SELECT col FROM table3
...
SELECT col FROM table100
) a