当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP MySQL - INNER JOIN: userid = part of value

问题描述:

Lets say I got this tables:

user:

userid | stuff

777 | bla

888 | bla2

34 | bla3

66 | bla4

visitors:

user_visitor |

777_34 |

888_34 |

777_66 |

What I need:

SELECT

*

FROM

visitors INNER JOIN user

ON

(the part after _ from user_visitor) = userid

WHERE

user_visitor LIKE '%777_%'

so when I parse userid and and stuff I get 34 / bla3 and 66 / bla4

How could I do that? Is it even possible?

网友答案:

This is what you want to do,

SELECT l1.stuff
FROM user_1 AS l1
INNER JOIN visitors AS l2
ON l2.user_visitor LIKE concat('%',l1.userid);

But I suggest you should split user_visitor to two column like userid and visitorid then you can add unique index them like this

ALTER TABLE visitors ADD UNIQUE unique_index(userid, visitorid);

Then your query like this.

SELECT l1.stuff
FROM user_1 AS l1
INNER JOIN visitors AS l2
ON l2.visitorid = l1.userid 

It is more efficient. Edit: You can do these in phpmyadmin.

分享给朋友:
您可能感兴趣的文章:
随机阅读: