English 中文(简体)
Mysql Cross Schema Queries
原标题:Mysql Cross Schema Queries Based on Row Data
  • 时间:2010-12-07 18:50:21
  •  标签:
  • sql
  • mysql

因此,Im公司在网络应用程序上工作,客户希望其用户数据与其他用户数据分开。 由于我们的客户可以相互协作,我们只能为每个客户建立一个新的申请案例,因为我们必须确保用户协会匹配,在客户之间独一无二。

基本上有我们的“应用”数据库,该数据库有一个“用户名录”表和数据库的名称,用以查找其余用户数据。 每个客户都有自己的数据库用户,只能查阅“申请”及其客户表格。

这里是它如何看待的一个实例。

CREATE DATABASE MasterDatabase
CREATE DATABASE Client1
CREATE DATABASE Client2

CREATE TABLE `MasterDatabase`.`Person` (
  `PersonId` INT NOT NULL AUTO_INCREMENT,
  `DatabaseName` Varchar(20) NOT NULL,
  PRIMARY KEY (`PersonId`)
)

CREATE TABLE `Client1`.`Person` (
      `PersonId` INT NOT NULL,
      `FirstName` Varchar(20) NOT NULL,
      PRIMARY KEY (`PersonId`)
    )

CREATE TABLE `Client2`.`Person` (
     `PersonId` INT NOT NULL,
     `FirstName` Varchar(20) NOT NULL,
      PRIMARY KEY (`PersonId`)
)

INSERT INTO MasterDatabase.Person VALUES (1,  Client1 );
INSERT INTO MasterDatabase.Person VALUES (2,  Client2 );

INSERT INTO Client1.Person VALUES (1,  Matt );
INSERT INTO Client2.Person VALUES (2,  Scott );

因此,客户1将有一个数据库用户,可以查阅总数据库和客户1表。 客户2将有一个用户可以进入总数据库和客户2。

我希望,我能以某种方式进行跨图式盘问,方便地利用主达塔基的“DatabaseName”领域的数据,但我可以把数据引出。 我知道,大部分情况下,我可以将客户数据库的名称储存在应用逻辑上,而只是把它插入询问中,但我不得不把几个地点合并为一个问题。

我的尝试就是这样做。

SELECT *, `DatabaseName` INTO @DB FROM MasterDatabase.Person 
LEFT JOIN @DB.Person ON MasterDatabase.Person.PersonId = @DB.Person.PersonId

但是,正如我所希望的那样,这只是徒劳的。 我能否用某种程序这样做? 我也愿意接受其他想法,即如果你有的话,将用户数据分开。

问题回答

我认为,你可以在储存程序中这样做。 它将研究类似的东西。

DELIMITER //
CREATE DEFINER= root @ localhost  PROCEDURE GetData(IN user_id INT)
BEGIN
  DECLARE db_name varchar(100);
  SELECT DatabaseName INTO db_name FROM MasterDatabase.Person WHERE  PersonId = user_id;
  SET @t1 =CONCAT( SELECT * FROM  ,db_name, .Person  );
  PREPARE stmt1 FROM @t1;
  EXECUTE stmt1;
END//

<>>>> Sorry, I forgot DEALLOCATE PREP rémt1;; 它应当放在EXECUTE之后。 段 次 页 次





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...