MySQL query with join, two tables with "id" field |
[eluser]internut[/eluser]
Hey All, I have which works great: Code: $data['query'] = $this->db->query("SELECT * FROM `users` WHERE `id` IN (SELECT `user_id` FROM `groups_exp` WHERE `group_id`='$filter_group' AND status='$user_status' AND $field LIKE '$keyword') ORDER BY $sort $direction LIMIT $start,$config[per_page] "); // query time Only issue i'm having is both tables have an "id" field. And on a LIKE search its grabbing the "id" field of the table i'm joining, not the original. I want if LIKE '$keyword' to be the "id" of table "users", not "groups_exp" Any thoughts?
[eluser]gullah[/eluser]
I had my tables set up like this and it will be a lot less confusing when handling results if you name your fields uniquely. It is a major pain when you are trying to pick out the users.id field when you could just have user_id and group_exp_id.
[eluser]internut[/eluser]
[quote author="drewtown" date="1228211206"]I had my tables set up like this and it will be a lot less confusing when handling results if you name your fields uniquely. It is a major pain when you are trying to pick out the users.id field when you could just have user_id and group_exp_id.[/quote] I questioned myself about this about having multiple tables with an "id" field for each record number. Is it a bad practice?
[eluser]gullah[/eluser]
you could do it with aliases or table.field, what is most important is that you follow a convention, be it yours or a standard. I personally like to use absolutely unique primary field names to avoid confusion and make sure there is no ambiguity. Take a look at this, provides some conventions and links to other standard conventions. http://justinsomnia.org/writings/naming_...tions.html
[eluser]kgill[/eluser]
Ok, let's see if we can't shed a little light on things here. First you're using an IN clause & subquery in place of a join, for the most part that's horribly inefficient. Second, the reason you're getting the id from the groups_exp is because that's what's in your subquery - let's dissect it a bit here: Code: SELECT * If you wanted your LIKE clause to apply the the users tables you'd need to move it out of the subquery. The other issue is the naming, as the others in this thread have said when you have two fields with the same name you should either reference the table name or use an alias. e.g. Code: SELECT users.name, groups_exp.name Finally, this is pretty basic SQL, if you plan on doing any further development the best thing you can do for yourself is to pick up a primer and read up a bit, you'll save yourself some major headaches. - K
[eluser]m4rw3r[/eluser]
You don't need to select things to be able to use them in a WHERE or ON clause. Select does only "filter" what to return.
[eluser]kgill[/eluser]
I think you're referring to my example there, I guess I didn't make it quite clear what I was attempting to show. You're correct, I was just tossing out a quick example of how to deal with two fields with the same name, it was late and I didn't consider how my choice of fields could be confusing. Fixed now, id's have become names. - K
[eluser]m4rw3r[/eluser]
In the case of names, the columns cannot have the same name or they will overwrite each other. Examples: Code: SELECT users.name, groups_exp.name FROM...
[eluser]internut[/eluser]
m4rw3r, Good post, very clear. I did wind up going through my code / tables and making all the ID fields unique just so this is not an issue. Thats the better way to go anyway. Now I'll work on doing the new join code today from the slop I originally had. |
Welcome Guest, Not a member yet? Register Sign In |