7

This may sound like an odd question, but I'm curious to know if it's possible...

Is there a way to simulate MySQL records using inline data? For instance, if it is possible, I would expect it to work something like this:

SELECT inlinedata.*
FROM (
  ('Emily' AS name, 26 AS age),
  ('Paul' AS name, 56 AS age)
) AS inlinedata
ORDER BY age
1
  • afaik you can create temporary tables and select from them Commented Nov 25, 2013 at 12:47

4 Answers 4

13

Unfortunately MySQL does not support the standard values row-constructor for this kind of things, so you need to use a "dummy" select for each row and combine the rows using UNION ALL

SELECT *
FROM (
  select 'Emily' AS name, 26 AS age 
  union all 
  select 'Paul', 56
) AS inlinedata
ORDER BY age

The UNION ALL serves two purposes

  1. It preserves any duplicate you might have on purpose
  2. It's a (tiny) bit faster than a plain UNION (because it does not check for duplicates)
Sign up to request clarification or add additional context in comments.

3 Comments

You don't actually need the wrapper.
@Strawberry: in this simplified example you don't that's true. But I think it's clearer that way (of course that's a personal preference) - and if you start joining that derived table you do need it.
Could you show how this works for a third row? I am trying but failing with that. It would help make the answer helpful for more scenarios.
0

No, not without making it complicated, but you can create a temporary table and query that instead. Temporary tables are deleted when the current client session terminates.

You can query them and insert data into them just like with other tables. When you create them, you have to use the TEMPORARY keyword, like so:

CREATE TEMPORARY TABLE ...

This way, you can also reuse the data for multiple queries if needed, no data gets stored, and all records that you query have the right structure (whereas the syntax you give in your example would create problems when you spell a column name wrong)...

Comments

0
with cte as (
    select '2012-04-04' as student_dob, '%test1%' as student_pat
    union all
    select '2012-05-04', '%test2%'
    union all
    select '2012-07-04', '%test3%'
    union all
    select '2012-05-11', '%test-n%'
)
select * 
from students s
inner join cte c
    on s.student_dob = c.student_dob and s.student_name like c.student_pat

arguably that's not a lot more readable, but taking a lead from that, you can just store those in a table or go through temporary table, like Roy suggested.

Also it's not great idea to make a group by student id and select also something else like you did in 2nd query.

Comments

0

create database Airline_Management_System; use Airline_Management_System;

create table aircraft(
     aircraft_idm int primary key,
     aircraft_name varchar(20) noot null,
     capacity int not null,
     manufacture varchar(30) not null,
     model varchar(10) not null,
     year_manufactured date not null
);

create table flight(

);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.