简体   繁体   English

通过使用PHP选择两个表在mysql中创建新的临时表

[英]Creating new temporary tables in mysql from selecting two table using PHP

I have two tables with different fields in that and having only one common field as primary key/foreign key. 我有两个具有不同字段的表,并且只有一个公共字段作为主键/外键。 How can i create temporary table with new fields in that, while selecting some of the fields from two existing tables? 从两个现有表中选择某些字段时,如何在其中创建带有新字段的临时表? And i need to insert values once after the temp table creation. 而且我需要在临时表创建后插入一次值。

Situation 1: I need to do this when i need to generate some reports from two existing tables in one single click like create table if not exists already, insert values in it, have to produce the result. 情况1:当我需要一次单击就可以从两个现有表生成一些报告时,例如创建表(如果尚不存在),在其中插入值,必须产生结果,就需要这样做。 have to do this all in single click. 只需单击一下即可完成所有操作。

situation 2: I have rank field in table 2. According to rank field, new fields can be created in the new temp table. 情况2:我在表2中有等级字段。根据等级字段,可以在新的临时表中创建新的字段。 For ex: if rank>1<100, then rank1 field has to be created in temp table. 例如:如果rank> 1 <100,则必须在临时表中创建rank1字段。 if rank >101<200, then rank2 field has to be created. 如果等级> 101 <200,则必须创建等级2字段。

Situation 3: If the select query generates number of records then i want to insert that in one row itself without making too many rows 情况3:如果选择查询生成了许多记录,那么我想将其插入一行而不会产生太多行

Ex: 例如:

If i am producing some results as the following way 如果我通过以下方式产生一些结果

Date rank 日期等级
11-01-2015 11 2015年11月11日11
11-01-2015 120 2015年11月11日120
11-01-2015 210 2015年11月11日210

i need to create temp table in such a manner to combine all the ranks with only one date field in it and with rank1=11, rank2=120 and rank3=210 我需要以这样的方式创建临时表,以将所有排名与其中只有一个日期字段以及rank1 = 11,rank2 = 120和rank3 = 210组合在一起

Then i can be able to get all ranks through the date in single query. 然后,我可以在单个查询中获得日期中的所有排名。

Situation 2: 情况2:

CREATE TEMPORARY TABLE tablename AS
SELECT date, 
       MAX(CASE WHEN rank BETWEEN 1 AND 100 THEN rank END) AS rank1,
       MAX(CASE WHEN rank BETWEEN 101 AND 200 THEN rank END) AS rank2,
       MAX(CASE WHEN rank BETWEEN 201 AND 300 THEN rank END) AS rank3,
       MAX(CASE WHEN rank > 300 THEN rank END) AS rank4
FROM table2
GROUP BY date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM