Php implements dynamic view code in MySQL

Source: Internet
Author: User

The system generates a unique ID (either the application or the uuid of the database tutorial) in some way, and then tries to associate it with this id.

Code:



Drop table if exists test;
Create table test (
Id int not null,
Name varchar (20) not null
);
Insert into test values (1, 'test1 ');
Insert into test values (1, 'test11 ');
Insert into test values (1, 'test111 ');
Insert into test values (2, 'test2 ');
Insert into test values (2, 'test22 ');
Drop function if exists getSpid;
Delimiter |
CREATE function getSpid ()
RETURNS int
RETURN @ spid;
|
Delimiter;
Drop view if exists v_test;
Create view v_test
Select * from test where id = getSpid ();
-- Test code
-- Enable session 1
Set @ spid = 1;
Select * from v_test;
-- Enable session 2
Set @ spid = 2;
Select * from v_test;


Note:

Maintain the generated ID in the session variable.

Create a UDF and return this variable.

Finally, call this function in the view.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.