Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
789 views
in Technique[技术] by (71.8m points)

sql - Joining on columns of different type?

If one column is of type int and say has a value 10. The other column is of type varchar and has a value of '10'. Is it safe to join on these values (mySql), and would I get the same result as if both were of type int and value 10?

I need to have them of different types because in one table the column is an autoincrementing key (so it must be int). The other column (in a different table) may occasionally contain values with letters so I must make it varchar. Can you forsee any huge problems with this or maybe I should rethink my schema?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two problems with this approach:

  1. MySQL will not use indexes on such join - which may or may not be a huge deal depending on your circumstances (table size / query / etc...)

  2. Database design-wise this smells fishy. Perhaps it's my poor imagination :-) but I can't think of an example where something like this would be justified. If it's a primary key in one table, it should be foreign key in another. Can you explain what you're trying to do in more detail?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...