Home > database >  For help: an SQL query problem, who taught (white) I am self-study
For help: an SQL query problem, who taught (white) I am self-study

Time:09-28

Consult a SQL query problem.
Laravel projects using
Select * from article where (article_id, $article) - & gt; get();
According to the SQL query out 7
Query the approve field value for | 1 | | 3 | | | 7 this form inside the value 1 3 7 is corresponding to a User table user_id
Demand is trying to approve this field 1 3 7 of these three values into the corresponding user_name Tom, dick and harry fifty
Excuse me can pass an SQL statement, came out to meet the requirements of data can query?
If you can't have what solutions?
Thank humbly

CodePudding user response:

Problem description is not clear that your existing data table, and you need to achieve the effect, posted at the best

CodePudding user response:


This picture is the article table user table is not convenient to post, but I believe that this should be able to make bosses read

CodePudding user response:

Suppose your user table fields as user_id, user_name, try the following code

The select aa. *, concat (" | ", ifnull (bb) user_name, "NA"), "| |", ifnull (cc) user_name, "NA"), "| |", ifnull (dd) user_name, "NA"), "|") approval_nm
From the article aa
Left the join user bb on SUBSTRING_INDEX (SUBSTRING_INDEX (aa) approval, "| |", 1), "|", 1)=bb. User_id
Left the join user cc on SUBSTRING_INDEX (SUBSTRING_INDEX (aa) approval, "| |", 2), "|", 1)=cc. User_id
Left the join user dd on SUBSTRING_INDEX (SUBSTRING_INDEX (aa) approval, "| |", 1), "|", 1)=dd. User_id
  • Related