本文介绍了想要显示来自具有相同ID的两个不同表的不同记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个表,其中一个是具有ID,名称,价格,说明的产品信息,另一个具有与1个ID相关的图像
想要只显示ID为ID且ID为1的图像的唯一记录,请不要重复
我正在使用此查询,但是获得重复ID的任何帮助

Hi
i have two tables one is having product info that is id,name, price, description other having different images related to 1 id
want to display only distinct record that is 1 image with id and id shouldnot repeat
i am using this query but getting repetative id''s any help

string cmdstr = "SELECT Productname,Price,Description,images,id,Sellername FROM Products AS a INNER JOIN Productimage AS p ON a.id = p.productid where Category='" + Label1.Text + "' ORDER BY a.id DESC";

推荐答案

string cmdstr = @"SELECT Productname,Price,Description,images,id,Sellername
FROM Products AS a INNER JOIN Productimage AS p ON a.id = p.productid
where Category='" + Label1.Text + "'
AND p.Ordinal = 1
ORDER BY a.id DESC";


附带说明,您永远不要将UI字段中的值连接到SQL语句.这使您可以进行SQL注入.有关更多信息,请参见正确执行数据库操作,版本3 [ ^ ]

加法:

例如:如果您想显示具有最小"名称的图像(无论该字符数据实际上意味着什么),那么您可以使用类似
的图像


As a side-note, you should never concatenate values from UI fields to the SQL statement. This leaves you wide open to SQL injections. For more information, see Properly executing database operations, version 3[^]

Addition:

As an example: If you would like to show the image having the ''minimum'' name (whatever that actually means with character data) then you could have something like

string cmdstr = @"SELECT Productname,Price,Description,images,id,Sellername
FROM Products AS a INNER JOIN Productimage AS p ON a.id = p.productid
where Category='" + Label1.Text + "'
AND NOT EXISTS (SELECT 1
                FROM ProductImage p2
                WHERE p2.ProductId = a.Id
                AND   p2.Images > p.Images)
ORDER BY a.id DESC";



这篇关于想要显示来自具有相同ID的两个不同表的不同记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 18:09
查看更多