简体   繁体   English

根据另一个表上的查询获取表的行

[英]Getting rows of a table based on query on another table

I have these 2 tables 我有这两张桌子

[Products]
ID
CompanyID,
Name,
PartNo,
IDSGroup,
ChartNo

[Company]
ID,
Name,
RegistrationNo,
RegistrationDate

I want to show these columns from tables when user search for a company name: 我想在用户搜索公司名称时显示表中的这些列:

Company.Name, Product.Name, Product.PartNo, Product.IDSGroup

This search query let me get the CompanyID 此搜索查询让我获得CompanyID

Select Company.ID from Company WHERE Company.Name LIKE "$userSearch%" 

Now I want to use Company.ID for this query to get all product 现在我想使用Company.ID来获取所有产品

Select * from Products WHERE CompanyID = "id from previous query"

If it's in the same database you can just join the two tables 如果它在同一个数据库中,您可以加入这两个表

SELECT
Company.Name, Products.Name, Products.PartNo, Products.IDSGroup
FROM
Company
LEFT JOIN Products
ON Company.ID = Products.CompanyID
WHERE
Company.Name LIKE "$userSearch%"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM