我创建了一个名为recommendations
的表,如下所示:
+----+---------------+---------------------+
| id | name | elements_to_announce |
+----+---------------+---------------------+
| 1 | Recomanation1 | 2 |
| 2 | Recomanation1 | 1,2 |
+----+---------------+---------------------+
另一个名为products
的表看起来像这样:
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | Low fat croissant |
| 2 | Yupi bread |
+----+-------------------+
现在我想用这两个表创建一个连接,它获取recommendations
的信息,但用产品名称替换elements_to_announce
。
必须是这样的:
+----+---------------+---------------------------------------+
| id | name | elements_to_announce |
+----+---------------+---------------------------------------+
| 1 | Recomanation1 | Yupi bread |
| 2 | Recomanation1 | Low fat croissant,Yupi bread |
+----+---------------+---------------------------------------+
我做了很多测试,而且几乎可以正常使用的代码是:
SELECT
r.id AS recommendation_id,
GROUP_CONCAT(p.id) AS product_id
FROM products AS p
JOIN recommendations AS r
GROUP BY r.id
此代码的问题在于它返回所有产品名称。
修改 我的“差不多”查询的结果:
+----+---------------+---------------------------------------+
| id | name | elements_to_announce |
+----+---------------+---------------------------------------+
| 1 | Recomanation1 | Low fat croissant,Yupi bread |
| 2 | Recomanation1 | Low fat croissant,Yupi bread |
+----+---------------+---------------------------------------+
答案 0 :(得分:0)
GROUP_CONCAT和FIND_IN_SET函数将用于实现它。
private void insertListIntoSQL(List<string[]> authList)
{
string connectionstring; //Connection String Info
SqlConnection cnn = new SqlConnection(connectionstring);
cnn.Open();
authList.RemoveAt(0);
foreach(string[] auth in authList)
{
SqlCommand updateTable = new SqlCommand("INSERT INTO AuthSubmitAppLog (RECEIVED, STATUS_COMMENTS, AUTHORIZATION_NUMBER, CASE_MANAGER, ENTER_DATE, UPDATE_DATE, COMPLETED_BY, COMPLETED_DATETIME, CONTRACT_ENROLL_DATE, CONTRACT_DISENROLL_DATE, DIAGNOSIS_CODE, AUTHORIZATION_TYPE, AUTHORIZATION_TYPE_DETAIL, MEMBER_NAME, MEMBER_ID, MEMBERSHIP_ID, MEMBER_ADDRESS_1, MEMBER_ADDRESS_2, MEMBER_CITY, MEMBER_STATE, MEMBER_ZIP, REGION, MEMBER_PHONE, LINE_OF_BUSINESS, MEDICAID_NUMBER, MEDICARE_NUMBER, MEDICARE_TYPE, VENDOR_NAME, MCO_PROVIDER_NAME, LOCATION_ID, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_CATEGORY, DETAIL_NUMBERS, SERVICE_CODE, MODIFIER_1, MODIFIER_2, MODIFIER_3, MODIFIER_4, SUNDAY_UNITS, MONDAY_UNITS, TUESDAY_UNITS, WEDNESDAY_UNITS, THURSDAY_UNITS, FRIDAY_UNITS, SATURDAY_UNITS, TOTAL_UNITS, NOTES_TO_PROVIDER, CASE_TYPE, STATUS, ALTERNATIVE_CASE_NUMBERS, SERVICES_CT_ID) VALUES (@RECEIVED, @STATUS_COMMENTS, @AUTHORIZATION_NUMBER, @CASE_MANAGER, @ENTER_DATE, @UPDATE_DATE, @COMPLETED_BY, @COMPLETED_DATETIME, @CONTRACT_ENROLL_DATE, @CONTRACT_DISENROLL_DATE, @DIAGNOSIS_CODE, @AUTHORIZATION_TYPE, @AUTHORIZATION_TYPE_DETAIL, @MEMBER_NAME, @MEMBER_ID, @MEMBERSHIP_ID, @MEMBER_ADDRESS_1, @MEMBER_ADDRESS_2, @MEMBER_CITY, @MEMBER_STATE,@ MEMBER_ZIP, @REGION, @MEMBER_PHONE, @LINE_OF_BUSINESS, @MEDICAID_NUMBER, @MEDICARE_NUMBER, @MEDICARE_TYPE, @VENDOR_NAME, @MCO_PROVIDER_NAME, @LOCATION_ID, @EFFECTIVE_DATE, @EXPIRATION_DATE, @SERVICE_CATEGORY, @DETAIL_NUMBERS, @SERVICE_CODE, @MODIFIER_1, @MODIFIER_2, @MODIFIER_3, @MODIFIER_4, @SUNDAY_UNITS, @MONDAY_UNITS, @TUESDAY_UNITS, @WEDNESDAY_UNITS, @THURSDAY_UNITS, @FRIDAY_UNITS, @SATURDAY_UNITS, @TOTAL_UNITS, @NOTES_TO_PROVIDER, @CASE_TYPE, @STATUS, @ALTERNATIVE_CASE_NUMBERS, @SERVICES_CT_ID)");
updateTable.Connection = cnn;
updateTable.Parameters.AddWithValue("@RECEIVED", SqlDbType.VarChar).Value = auth[0].Normalize();
updateTable.Parameters.AddWithValue("@STATUS_COMMENTS", SqlDbType.VarChar).Value = auth[1].Normalize();
updateTable.Parameters.AddWithValue("@AUTHORIZATION_NUMBER", SqlDbType.VarChar).Value = auth[2].Normalize();
updateTable.Parameters.AddWithValue("@CASE_MANAGER", SqlDbType.VarChar).Value = auth[3].Normalize();
updateTable.Parameters.AddWithValue("@ENTER_DATE", SqlDbType.DateTime).Value = Convert.ToDateTime(auth[4]).ToString().Normalize();
updateTable.Parameters.AddWithValue("@UPDATE_DATE", SqlDbType.DateTime).Value = Convert.ToDateTime(auth[5]).ToString().Normalize();
updateTable.Parameters.AddWithValue("@COMPLETED_BY", SqlDbType.VarChar).Value = auth[6].Normalize();
updateTable.Parameters.AddWithValue("@COMPLETED_DATETIME", SqlDbType.DateTime).Value = auth[7].Normalize();
updateTable.Parameters.AddWithValue("@CONTRACT_ENROLL_DATE", SqlDbType.DateTime).Value = Convert.ToDateTime(auth[8]).ToString().Normalize();
updateTable.Parameters.AddWithValue("@CONTRACT_DISENROLL_DATE", SqlDbType.DateTime).Value = Convert.ToDateTime(auth[9]).ToString().Normalize();
updateTable.Parameters.AddWithValue("@DIAGNOSIS_CODE", SqlDbType.VarChar).Value = auth[10].Normalize();
updateTable.Parameters.AddWithValue("@AUTHORIZATION_TYPE", SqlDbType.VarChar).Value = auth[11].Normalize();
updateTable.Parameters.AddWithValue("@AUTHORIZATION_TYPE_DETAIL", SqlDbType.VarChar).Value = auth[12].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_NAME", SqlDbType.VarChar).Value = auth[13].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_ID", SqlDbType.VarChar).Value = auth[14].Normalize();
updateTable.Parameters.AddWithValue("@MEMBERSHIP_ID", SqlDbType.VarChar).Value = auth[15].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_ADDRESS_1", SqlDbType.VarChar).Value = auth[16].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_ADDRESS_2", SqlDbType.VarChar).Value = auth[17].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_CITY", SqlDbType.VarChar).Value = auth[18].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_STATE", SqlDbType.VarChar).Value = auth[19].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_ZIP", SqlDbType.VarChar).Value = auth[20].Normalize();
updateTable.Parameters.AddWithValue("@REGION", SqlDbType.VarChar).Value = auth[21].Normalize();
updateTable.Parameters.AddWithValue("@MEMBER_PHONE", SqlDbType.VarChar).Value = auth[22].Normalize();
updateTable.Parameters.AddWithValue("@LINE_OF_BUSINESS", SqlDbType.VarChar).Value = auth[23].Normalize();
updateTable.Parameters.AddWithValue("@MEDICAID_NUMBER", SqlDbType.VarChar).Value = auth[24].Normalize();
updateTable.Parameters.AddWithValue("@MEDICARE_NUMBER", SqlDbType.VarChar).Value = auth[25].Normalize();
updateTable.Parameters.AddWithValue("@MEDICARE_TYPE", SqlDbType.VarChar).Value = auth[26].Normalize();
updateTable.Parameters.AddWithValue("@VENDOR_NAME", SqlDbType.VarChar).Value = auth[27].Normalize();
updateTable.Parameters.AddWithValue("@MCO_PROVIDER_NAME", SqlDbType.VarChar).Value = auth[28].Normalize();
updateTable.Parameters.AddWithValue("@LOCATION_ID", SqlDbType.VarChar).Value = auth[29].Normalize();
updateTable.Parameters.AddWithValue("@EFFECTIVE_DATE", SqlDbType.DateTime).Value = auth[30].Normalize();
updateTable.Parameters.AddWithValue("@EXPIRATION_DATE", SqlDbType.DateTime).Value = auth[31].Normalize();
updateTable.Parameters.AddWithValue("@SERVICE_CATEGORY", SqlDbType.VarChar).Value = auth[32].Normalize();
updateTable.Parameters.AddWithValue("@DETAIL_NUMBERS", SqlDbType.VarChar).Value = auth[33].Normalize();
updateTable.Parameters.AddWithValue("@SERVICE_CODE", SqlDbType.VarChar).Value = auth[34].Normalize();
updateTable.Parameters.AddWithValue("@MODIFIER_1", SqlDbType.VarChar).Value = auth[35].Normalize();
updateTable.Parameters.AddWithValue("@MODIFIER_2", SqlDbType.VarChar).Value = auth[36].Normalize();
updateTable.Parameters.AddWithValue("@MODIFIER_3", SqlDbType.VarChar).Value = auth[37].Normalize();
updateTable.Parameters.AddWithValue("@MODIFIER_4", SqlDbType.VarChar).Value = auth[38].Normalize();
updateTable.Parameters.AddWithValue("@SUNDAY_UNITS", SqlDbType.VarChar).Value = auth[39].Normalize();
updateTable.Parameters.AddWithValue("@MONDAY_UNITS", SqlDbType.VarChar).Value = auth[40].Normalize();
updateTable.Parameters.AddWithValue("@TUESDAY_UNITS", SqlDbType.VarChar).Value = auth[41].Normalize();
updateTable.Parameters.AddWithValue("@WEDNESDAY_UNITS", SqlDbType.VarChar).Value = auth[42].Normalize();
updateTable.Parameters.AddWithValue("@THURSDAY_UNITS", SqlDbType.VarChar).Value = auth[43].Normalize();
updateTable.Parameters.AddWithValue("@FRIDAY_UNITS", SqlDbType.VarChar).Value = auth[44].Normalize();
updateTable.Parameters.AddWithValue("@SATURDAY_UNITS", SqlDbType.VarChar).Value = auth[45].Normalize();
updateTable.Parameters.AddWithValue("@TOTAL_UNITS", SqlDbType.VarChar).Value = auth[46].Normalize();
updateTable.Parameters.AddWithValue("@NOTES_TO_PROVIDER", SqlDbType.VarChar).Value = auth[47].Normalize();
updateTable.Parameters.AddWithValue("@STATUS", SqlDbType.VarChar).Value = auth[48].Normalize();
updateTable.Parameters.AddWithValue("@CASE_TYPE", SqlDbType.VarChar).Value = auth[49].Normalize();
updateTable.Parameters.AddWithValue("@ALTERNATIVE_CASE_NUMBERS", SqlDbType.VarChar).Value = auth[50].Normalize();
updateTable.Parameters.AddWithValue("@SERVICES_CT_ID", SqlDbType.VarChar).Value = auth[51].Normalize();
updateTable.ExecuteNonQuery(); //<-- Error occurs here.
}
cnn.Close();
}
答案 1 :(得分:0)
问题在于您似乎以不适合的方式使用关系数据库。表recommendations
违反了所谓first normal form的条件。我建议将其拆分为两个表:
recommendations
:id
,name
recommendationContents
:recommendation_id
,product_id
这就是您的查询的样子:
SELECT r.id, r.name, GROUP_CONCAT(p.name)
FROM recommendationContents AS rc
JOIN recommendations AS r ON rc.recommendation_id = r.id
JOIN products AS p ON rc.product_id = p.id
GROUP BY r.id, r.name