Sql 在数据库中保存其他ID
我这里有两张桌子:Sql 在数据库中保存其他ID,sql,database,Sql,Database,我这里有两张桌子: 1. tblPurItem : itemID, categoryID, purchaseMeasurementID, stockMeasurementID, calculationMeasurementID, retailrecipeID 2. tblRecipe : recipeID, recipeName, recipeType *retailRecipeID = recipeID 如何获取tblPurItem中的retailRecipeI
1. tblPurItem :
itemID, categoryID, purchaseMeasurementID, stockMeasurementID, calculationMeasurementID, retailrecipeID
2. tblRecipe :
recipeID, recipeName, recipeType
*retailRecipeID = recipeID
如何获取tblPurItem中的retailRecipeID值?因为如果我从tblPosRecipe D中添加like SELECT D.recipeName,其中A.retailRecipeID=D.recipeID LIMIT 1作为recipeName,recipeName为null,即没有数据。我必须得到这份工作,因为在这种情况下,我有状态。表1为tblPurItem,表2为tblRecipe。中一没有问题,但中二有问题
当我成功提交表格1时,表格2将在radiobutton=yes上弹出,以便表格2成功提交数据。现在,我的问题是数据表2将丢失,因为不与表1链接。数据库中的retailRecipeID也为null。有人知道解决办法吗?谢谢大家!
应该是当我打开表格1和表格2时,数据将显示回来,但它显示为空白,而不是以前的数据
SQL
在没有匹配配方的情况下,使用tblRecipe和coalesce函数的左连接在表示层中获得值0
select
coalesce(t2.recipeID, 0) as recipeID
, t1.itemID
, t1.categoryID
, t1.purchaseMeasurementID
, t1.stockMeasurementID
, t1.calculationMeasurementID
, t1.itemName
, t1.itemDescription
, t1.itemPrice
, t1.lastPrice
, t1.averagePrice
, t1.itemType
, t1.purchaseStatus
, t1.cutStock
, t1.chargeOut
, t1.retailItem
, t1.consignment
, t1.marginConsignment
, t1.active
, (SELECT B.itemCategoryName FROM tblPurItemCategory B WHERE t1.categoryID = B.itemcategoryID LIMIT 1) AS itemCategoryName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.purchaseMeasurementID= C.measurementID LIMIT 1) AS purchasemeasurementName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.stockMeasurementID= C.measurementID LIMIT 1) AS stockmeasurementName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.calculationMeasurementID= C.measurementID LIMIT 1) AS calculationmeasurementName
from tblPurItem t1
left join tblRecipe t2 on t1.retailrecipeID = t2.recipeID
在没有匹配配方的情况下,使用tblRecipe和coalesce函数的左连接在表示层中获得值0
select
coalesce(t2.recipeID, 0) as recipeID
, t1.itemID
, t1.categoryID
, t1.purchaseMeasurementID
, t1.stockMeasurementID
, t1.calculationMeasurementID
, t1.itemName
, t1.itemDescription
, t1.itemPrice
, t1.lastPrice
, t1.averagePrice
, t1.itemType
, t1.purchaseStatus
, t1.cutStock
, t1.chargeOut
, t1.retailItem
, t1.consignment
, t1.marginConsignment
, t1.active
, (SELECT B.itemCategoryName FROM tblPurItemCategory B WHERE t1.categoryID = B.itemcategoryID LIMIT 1) AS itemCategoryName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.purchaseMeasurementID= C.measurementID LIMIT 1) AS purchasemeasurementName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.stockMeasurementID= C.measurementID LIMIT 1) AS stockmeasurementName
, (SELECT C.measurementName FROM tblPurMeasurement C WHERE t1.calculationMeasurementID= C.measurementID LIMIT 1) AS calculationmeasurementName
from tblPurItem t1
left join tblRecipe t2 on t1.retailrecipeID = t2.recipeID
示例数据、所需结果和适当的数据库标记都会有帮助。好的,我将更新更多示例数据、所需结果和适当的数据库标记都会有帮助。好的,我将更新更多我收到错误在我的数据库中运行此操作:Query:select,coalescet2.recipeID,0 as recipeID,t1.itemID,t1.categoryID,t1.purchaseMeasurementID,t1.库存测量,t。。。错误代码:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第行“coalescet2.recipeID,0作为recipeID,t1.itemID,t1.categoryID”附近使用的正确语法2@Alphabet,尝试一个增益,现在很好,但我可以知道为什么使用合并吗?为了补充我的知识,我们使用左连接,所以有可能会得到null值,如果recipeID为null,coalesce会给你0好的,我知道了。非常感谢你!我在数据库中运行此操作时出错:查询:select,coalescet2.recipeID,0作为recipeID,t1.itemID,t1.categoryID,t1.purchaseMeasurementID,t1.stockMeasurementID,t。。。错误代码:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第行“coalescet2.recipeID,0作为recipeID,t1.itemID,t1.categoryID”附近使用的正确语法2@Alphabet,尝试一个增益,现在很好,但我可以知道为什么使用合并吗?为了补充我的知识,我们使用左连接,所以有可能会得到null值,如果recipeID为null,coalesce会给你0好的,我知道了。非常感谢你!