| |

VerySource

 Forgot password?
 Register
Search
View: 17|Reply: 7

Help database query problem ...

[Copy link]

2

Threads

5

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-3-13 19:30:01
| Show all posts |Read mode
There is a data table similar to the following.
Table I.
ID1 Name KeyId
1 a1 2 | 22 | 3 | 5
2 a2 5 | 22 | 33 | 1
3 a3 8 | 10 | 2 | 1
4 a4 9 | 10 | 11 | 22
5 a5 2
Table II
ID2 text
1 123
2 456
3 789
4 1112

The KeyId stores the IDs of Table 2 separated by "|" characters. Assuming that Table 2 has ID2 = 2, select the record with KeyId 2 in Table 1.
Conversely, if there is no 2 in KeyId, write 2 in it and separate it with "|".

Ask everyone to point out the code.
Reply

Use magic Report

0

Threads

7

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

Post time: 2020-6-12 14:30:01
| Show all posts
Modify Table 1 to
ID1 Name KeyId
1 a1 2,22,3,5
2 a2 5,22,33,1
3 a3 8,10,2,1
4 a4 9,10,11,22
5 a5 2

Everything is solved
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-6-14 12:45:01
| Show all posts
$KeyId=split('|',$rs);
if(!array_key_exists($ID2,$KeyId)) $rs.='|'.$ID2;
Reply

Use magic Report

2

Threads

5

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-6-20 12:15:01
| Show all posts
How to write SQL statements changed to "," or "|"?
Reply

Use magic Report

2

Threads

5

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-6-20 17:15:01
| Show all posts
What should I do if there is 2 in KeyId?
Reply

Use magic Report

2

Threads

5

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-6-20 22:00:01
| Show all posts
$KeyId=split('|',$rs);
if(!array_key_exists($ID2,$KeyId)) $rs.='|'.$ID2;


It seems that even 22 is included.
Reply

Use magic Report

0

Threads

26

Posts

15

Credits

Newbie

Rank: 1

Credits
15

 China

Post time: 2020-6-22 09:30:01
| Show all posts
Well, use a comma.
Reply

Use magic Report

0

Threads

7

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-6-22 11:15:01
| Show all posts
select find_in_set(2,KeyId) from Table 1

Change | to, so that you can find out which has 2 and which does not, and then perform the operation you want
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list