This topic is locked

SQL Views and pictures that works

7/30/2023 5:03:40 AM
PHPRunner General questions
dageci authorDevClub member

I was searching the forum for a way to be able to show pictures from a view that is having UNION ALL.
(So this is also a not to myself in the future, if I forgot how to do it :-) )
All the answers were that it is not posible, that for this to be able we need a primary key.
But there is a way to do it. Just a note, these views are not updatable.
You only need to add a unique field in this view, like this:
ROW_NUMBER() OVER () AS unique_idHere is an example:
`-- Create table orderpictures1
CREATE TABLE orderpictures1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
picture MEDIUMTEXT NOT NULL
);

-- Create table orderpictures2
CREATE TABLE orderpictures2 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
picture MEDIUMTEXT NOT NULL
);

-- Create UNION ALL view all_order_pictures_view
CREATE OR REPLACE VIEW all_order_pictures_view AS
SELECT
id,
name,
picture,
ROW_NUMBER() OVER () AS unique_id
FROM
(
SELECT id, name, picture FROM orderpictures1
UNION ALL
SELECT id, name, picture FROM orderpictures2
) AS unioned_pictures;
`

dageci authorDevClub member 7/30/2023

And don't forget to tick that in this view the field "unique_id" is the primary key in the Choose key columns
img alt