如何使用子查询对选定列使用 where 条件?

发布于 2024-08-30 12:28:33 字数 460 浏览 4 评论 0原文

我有两栏:公司和产品。

我使用以下查询来获取与特定字符串匹配的产品...

select id,(select name from company where product.cid=company.id) as 
company,name,selling_price,mrp from product where name like '$qry_string%'

但是当我需要列出特定公司的产品时我该怎么办?

我尝试了以下方法,但本质上

select id,(select name from company where product.cid=company.id) as
company,name,selling_price,mrp from product where company like '$qry_string%'

帮助我

I have two columns as company and product.

I use the following query to get the products matching particular string...

select id,(select name from company where product.cid=company.id) as 
company,name,selling_price,mrp from product where name like '$qry_string%'

But when i need to list products of specific company how can i do?

i tried the following but in vein

select id,(select name from company where product.cid=company.id) as
company,name,selling_price,mrp from product where company like '$qry_string%'

Help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-09-06 12:28:33

您尝试执行的操作不需要子查询,简单的连接就足够了。试试这个:

select c.name, p.id, p.name, p.selling_price, p.mrp
  from company c
 inner join product p
    on c.id = p.cid
 where c.name like '$qry_string%'

我认为您尝试的查询的问题是您不能在 where 子句中使用作为子查询(在您的情况下为“company”)结果的字段。您可以尝试拥有

What you are trying to do does not require a subquery, a simple join is enough. Try this:

select c.name, p.id, p.name, p.selling_price, p.mrp
  from company c
 inner join product p
    on c.id = p.cid
 where c.name like '$qry_string%'

I think the problem with the query you tried is that you cannot use fields that are the result of a subquery (in your case, "company") in the where clause. You might try having instead.

寄人书 2024-09-06 12:28:33

您可以使用

SELECT p.id, c.name AS company, p.name, p.selling_price, p.mrp FROM product p, company c WHERE p.cid=c.id AND c.name LIKE '$qry_string'

You can use

SELECT p.id, c.name AS company, p.name, p.selling_price, p.mrp FROM product p, company c WHERE p.cid=c.id AND c.name LIKE '$qry_string'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文