Home Ask Login Register

Developers Planet

Your answer is one click away!

Danielphillips February 2016

MySql how to avoid having to create temporary tables

I'm very new to SQL and have a query that I need to use through a WordPress plugin (WP Data Tables) but the plugin doesn't allow the creation of temporary tables.

To work around this I believe I have to create new tables within a join but I'm not sure how to do this.

The query I need to remove the creation of table from is:

create temporary table if not exists _SurveyData AS

(SELECT 
    _RefTable.RefID,
    _RefTable.ClientRef,
    _RefTable.SiteName,
    _JobTable.JobID,
    _JobTable.JobTypeID,
    _JobTable.JobClosedDate 
FROM
    _RefTable, _JobTable
WHERE
    _RefTable.RefID = _JobTable.RefID and  _JobTable.JobTypeID = 1);

create temporary table if not exists _InstallData AS

(SELECT 
    _RefTable.RefID,
    _RefTable.ClientRef,
    _RefTable.SiteName,
    _JobTable.JobID,
    _JobTable.JobTypeID,
    _JobTable.JobClosedDate 
FROM
    _RefTable, _JobTable
WHERE
    _RefTable.RefID = _JobTable.RefID and  _JobTable.JobTypeID = 2);

SELECT
    _RefTable.RefID,
    _RefTable.ClientRef,
    _RefTable.SiteName,
    _SurveyData.JobID,
    _SurveyData.JobClosedDate,
    _InstallData.JobID,
    _InstallData.JobClosedDate
from
    _RefTable, _SurveyData, _InstallData
Where
    _RefTable.RefID = _SurveyData.RefID and _RefTable.RefID = _InstallData.RefID
    order by RefID

Any help or a point in the right direction would be greatly appreciated.

Answers


Code-Monk February 2016

Changed Temporary table into sub queries.

Try this:

SELECT _RefTable.RefID,
    _RefTable.ClientRef,
    _RefTable.SiteName,
    _SurveyData.JobID,
    _SurveyData.JobClosedDate,
    _InstallData.JobID,
    _InstallData.JobClosedDate
from RefTable
INNER JOIN(
            SELECT 
            _RefTable.RefID,
            _RefTable.ClientRef,
            _RefTable.SiteName,
            _JobTable.JobID,
            _JobTable.JobTypeID,
            _JobTable.JobClosedDate 
                FROM RefTable, _JobTable WHERE _RefTable.RefID = _JobTable.RefID and  _JobTable.JobTypeID = 1
            ) as _SurveyData on _RefTable.RefID = _SurveyData.RefID 
INNER JOIN(
            SELECT 
            _RefTable.RefID,
            _RefTable.ClientRef,
            _RefTable.SiteName,
            _JobTable.JobID,
            _JobTable.JobTypeID,
            _JobTable.JobClosedDate 
            FROM  _RefTable, _JobTable WHERE _RefTable.RefID = _JobTable.RefID and  _JobTable.JobTypeID = 2
        ) as _InstallData on _RefTable.RefID = _InstallData.RefID
order by RefID;

Post Status

Asked in February 2016
Viewed 2,168 times
Voted 6
Answered 1 times

Search




Leave an answer


Quote of the day: live life