[英]Retrieve data from one table and insert/update in another table in mysql
[英]Retrieve data from one MySQL table and use it in another one
我在jqgrid中使用了兩個表。
Table 1: id->0,1,2; state->1,1,0
Table 2: id->0,1,2,3; product->apple,banana,cherry,melon;
我想找到狀態為1的ID,並從另一張表中檢索相應的產品。 有適當的方法嗎? 低於$ result始終返回零。 我已成功連接到數據庫。
這是代碼:
$var = "SELECT id FROM table1 WHERE state = 1";
$result = mysql_query($var);
$grid->setSelect('order', "SELECT id, product FROM table2 WHERE id='$result' ");
您可以運行一個查詢:
SELECT t2.id, t2.product
FROM table2 t2
WHERE t2.id = (SELECT t1.id FROM table1 t1 WHERE t1.state = 1);
如果子查詢可以想象返回多個結果,則使用in
代替=
:
SELECT t2.id, t2.product
FROM table2 t2
WHERE t2.id IN (SELECT t1.id FROM table1 t1 WHERE t1.state = 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.