[英]Bring the Options from database table in 2nd Select box based on the value selected in 1st Select Box
[英]How to select data of 2nd table from column values of 1st table in same select statement?
假設我有這個查詢:
SELECT first, last, operation_code, serial_no FROM list_data WHERE cid='".$cid."'
並假設查詢結果是這樣的:
john, smith, 100, 000000001
john, doe, 200, 000000002
jane, doe, 300, 000000003
我想向其他表格的結果中添加一些數據。 像這樣:
john, smith, 100 - description for 100, 000000001
john, doe, 200 - description for 200, 000000002
jane, doe, 300 - description for 300, 000000003
另一個表具有一個operation_code列和一個稱為operation_text的描述列。 因此,當原始查詢正在通過第一個表來獲取記錄時,它是否可以使用operation_code號來從operation_codes表中獲取該操作代碼的描述?
不知道這是否重要,但是它是否需要適合我使用的這種csv文件創建方法。
$output = fopen('php://output', 'w');
fputcsv($output, array('first', 'last', 'operation_code', 'serial_no'));
$rows = mysql_query("SELECT first, last, operation_code, serial_no FROM list_data WHERE cid='".$cid."' ORDER BY last ASC, first ASC");
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
因此,在創建csv文件時,我希望代碼號和描述在同一列中。 在代碼和描述之間沒有多余的逗號,除非沒有其他方法,那么如果將描述放在自己的列中就可以了,例如:
Header line of csv:
first, last, operation_code, operation_text, serial_no
body of csv:
john, smith, 100, description for 100, 000000001
john, doe, 200, description for 200, 000000002
jane, doe, 300, description for 300, 000000003
這只是第二張表的INNER JOIN:
SELECT first, last, list_data.operation_code, second_table.operation_text, serial_no
FROM list_data INNER JOIN second_table ON list_data.operation_code = second_table.operation_code
WHERE cid='".$cid."'
在這里,我們加入list_data
到second_table
在operation_code
再向上的SELECT
,我們在新的領域帶來的聲明部分。 請注意,對於operation_code
因為它存在於查詢中的兩個表中,所以我們必須指定所需的operation_code
來自list_data
表(由於它是一個INNER JOIN,所以兩個都可以使用,但必須指定)。
如果您還希望滿足在同一列中返回operation_code和operation_text的要求,則可以將這兩者簡單地串聯起來:
SELECT first, last, CONCAT(list_data.operation_code,' - ', second_table.operation_text) AS operation, serial_no
FROM list_data INNER JOIN second_table ON list_data.operation_code = second_table.operation_code
WHERE cid='".$cid."'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.