虽然我基本上同意我在这里看到的其他答案,但我将对我的答案采取不同的策略:
如果您正在尝试学习 SQL,那么您必须训练自己使用更好的格式。这可能看起来很琐碎,但实际上,如果您使用更易于理解的格式 ,您会发现它很容易阅读、理解,尤其是编辑。
虽然我真的很怀念 c# 中的 c/c++ 类型块引用,但几十年的 VB 编程已经使我适应了没有它们的这种有点丑陋的要求。比较你的例子:
txt = "SELECT * from D1Table WHERE Synopsis LIKE '%" + txtBText + "%' OR Author1 LIKE '%" + txtBText + "%' OR Author2 LIKE '%" + txtBText + "%' OR Author3 LIKE '%" + txtBText + "%' OR Author4 LIKE '%" + txtBText + "%' OR Author5 LIKE '%" + txtBText + "%' OR Biography1 LIKE '%" + txtBText + "%' OR Biography2 LIKE '%" + txtBText + "%' OR Biography3 LIKE '%" + txtBText + "%' OR Biography4 LIKE '%" + txtBText + "%' OR Biography5 LIKE '%" + txtBText + "%' OR Title LIKE '%" + txtBText + "% ' OR Position1 LIKE '%" + txtBText + "%'OR Position2 LIKE '%" + txtBText + "%' OR Position3 LIKE '%"+ txtBText + "%' OR Position4 LIKE '%" + txtBText + "%' OR Position5 LIKE '%" + txtBText + "%' ";
相当于:
txt = ""
+ " SELECT * "
+ " from D1Table "
+ " WHERE Synopsis LIKE '%" + txtBText + "%' "
+ " OR Author1 LIKE '%" + txtBText + "%' "
+ " OR Author2 LIKE '%" + txtBText + "%' "
+ " OR Author3 LIKE '%" + txtBText + "%' "
+ " OR Author4 LIKE '%" + txtBText + "%' "
+ " OR Author5 LIKE '%" + txtBText + "%' "
+ " OR Biography1 LIKE '%" + txtBText + "%' "
+ " OR Biography2 LIKE '%" + txtBText + "%' "
+ " OR Biography3 LIKE '%" + txtBText + "%' "
+ " OR Biography4 LIKE '%" + txtBText + "%' "
+ " OR Biography5 LIKE '%" + txtBText + "%' "
+ " OR Title LIKE '%" + txtBText + "%' "
+ " OR Position1 LIKE '%" + txtBText + "%' "
+ " OR Position2 LIKE '%" + txtBText + "%' "
+ " OR Position3 LIKE '%" + txtBText + "%' "
+ " OR Position4 LIKE '%" + txtBText + "%' "
+ " OR Position5 LIKE '%" + txtBText + "%' "
;
显然更容易阅读和理解。没有立即明显的是,它也更容易编辑。考虑您需要通过添加 UNION 来检查两个表:
txt = ""
// <CUT from here
+ " SELECT * "
+ " from D1Table "
+ " WHERE Synopsis LIKE '%" + txtBText + "%' "
+ " OR Author1 LIKE '%" + txtBText + "%' "
+ " OR Author2 LIKE '%" + txtBText + "%' "
+ " OR Author3 LIKE '%" + txtBText + "%' "
+ " OR Author4 LIKE '%" + txtBText + "%' "
+ " OR Author5 LIKE '%" + txtBText + "%' "
+ " OR Biography1 LIKE '%" + txtBText + "%' "
+ " OR Biography2 LIKE '%" + txtBText + "%' "
+ " OR Biography3 LIKE '%" + txtBText + "%' "
+ " OR Biography4 LIKE '%" + txtBText + "%' "
+ " OR Biography5 LIKE '%" + txtBText + "%' "
+ " OR Title LIKE '%" + txtBText + "%' "
+ " OR Position1 LIKE '%" + txtBText + "%' "
+ " OR Position2 LIKE '%" + txtBText + "%' "
+ " OR Position3 LIKE '%" + txtBText + "%' "
+ " OR Position4 LIKE '%" + txtBText + "%' "
+ " OR Position5 LIKE '%" + txtBText + "%' "
// CUT to here>
// VV add a UNION
+ " UNION ALL "
// <PASTE here
+ " SELECT + "
+ " from D2Table " // <-- change the table name here
+ " WHERE Synopsis LIKE '%" + txtBText + "%' "
+ " OR Author1 LIKE '%" + txtBText + "%' "
+ " OR Author2 LIKE '%" + txtBText + "%' "
+ " OR Author3 LIKE '%" + txtBText + "%' "
+ " OR Author4 LIKE '%" + txtBText + "%' "
+ " OR Author5 LIKE '%" + txtBText + "%' "
+ " OR Biography1 LIKE '%" + txtBText + "%' "
+ " OR Biography2 LIKE '%" + txtBText + "%' "
+ " OR Biography3 LIKE '%" + txtBText + "%' "
+ " OR Biography4 LIKE '%" + txtBText + "%' "
+ " OR Biography5 LIKE '%" + txtBText + "%' "
+ " OR Title LIKE '%" + txtBText + "%' "
+ " OR Position1 LIKE '%" + txtBText + "%' "
+ " OR Position2 LIKE '%" + txtBText + "%' "
+ " OR Position3 LIKE '%" + txtBText + "%' "
+ " OR Position4 LIKE '%" + txtBText + "%' "
+ " OR Position5 LIKE '%" + txtBText + "%' "
// ^^ change these column names if needed
;
这里剩下的是对于 UNION,SELECT 必须返回相同数量的列,并且列的数据类型必须兼容。因此,如果 D1Table 和 D2Table 具有不同的列,那么您必须删除“*”并为每个 SELECT 创建兼容的列列表。