English 中文(简体)
MySQL 查询中的用户定义变量
原标题:user defined variable in MySQL query

I m trying to create a data abstraction in SQL where I can choose the quarter I want to look at for my employee info. Right now I m using the naming system Q1_P1_N for "Quarter 1 Project 1 name" and Q1_P1_W is "...project 1 weight." Employees can work on multiple projects. So far what I have is:

CREATE PROCEDURE effort_lookup4(IN proj_name VARCHAR(20), IN quarter INT(1))
BEGIN
SET @Qx_P1_N = CONCAT( Q , quarter,  _P1_N );
SET @Qx_P2_N = CONCAT( Q , quarter,  _P2_N );
SET @Qx_P1_W = CONCAT( Q , quarter,  _P1_W );
SET @Qx_P2_W = CONCAT( Q , quarter,  _P2_W );
SET @var1 = (SELECT sum(@Qx_P1_W) FROM table_test WHERE @Qx_P1_N = proj_name);
SET @var2 = (SELECT sum(@Qx_P2_W) FROM table_test WHERE @Qx_P2_N = proj_name);

我的问题是,每当我调用“%x_P1_N”或“%x_P1_W”的查询时,我实际上不会通过正确的查询,我无法找出我做错了什么。这应该很容易。我只是新来使用 SQL 。

这里请举一个表格外观的例子, 除了它会穿插到 Q2_P1_N,

+------+---------+---------+---------+---------+
| EMPID| Q1_P1_N | Q1_P2_N | Q1_P1_W | Q1_P2_W |
+------+---------+---------+---------+---------+
| 1000 | ProjA   | ProjB   |    0.50 |    0.50 |
| 1001 | ProjA   | NULL    |    1.00 |    NULL |
| 1010 | ProjB   | NULL    |    1.00 |    NULL |
| 1011 | ProjA   | ProjB   |    0.50 |    0.50 |
+------+---------+---------+---------+---------+

谢谢 谢谢

最佳回答

要使用您现有的数据结构,您需要使用 < a href="http://dev.mysql.com/doc/en/sql-syntax- prepared-statements.html" rel=“nofollow” > 准备声明 (您将您想要的 SELECT 声明构建为字符串,然后您将该声明构建为 PREPARE EXECUTE )。

然而,你可能会发现更容易改变你的数据结构:

+------+---------+---------+--------+
| EMPID| quarter | project | weight |
+------+---------+---------+--------+
| 1000 |       1 | A       |   0.50 |
| 1000 |       1 | B       |   0.50 |
| 1001 |       1 | A       |   1.00 |
| 1010 |       1 | B       |   1.00 |
| 1011 |       1 | A       |   0.50 |
| 1011 |       1 | B       |   0.50 |
+------+---------+---------+--------+
问题回答

在您选择的语句中,字段中有一个变量,该变量应该是表格中的列名称。您需要切换 Q1_P1_W 的 {x_P1_W} 。





相关问题
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 ...

热门标签