Welcome Guest, Not a member yet? Register   Sign In
Simplify a db query using active record syntax
#1

[eluser]boony[/eluser]
Hi,

I have a simple problem which is greater than my simple mind can handle.

Essentially, I want to remove duplicate entries from a table and insert the result into a new table. The following query works a treat:

$this->db->query('INSERT INTO courses SELECT DISTINCT coursecode, coursename FROM tempcourse');

The origin was a csv file which I read into tempcourse. The new table 'courses' is created via dbforge and the above query is executed and results are as expected.

But surely this can be achieved by using active record syntax. I've tried a few different passes but nothing works except the above query!

Any suggestions to simplify the above would be gratefully appreciated.
#2

[eluser]tomcode[/eluser]
Stick with Your query, there's nothing to simplify.
#3

[eluser]boony[/eluser]
Hmmmm! I felt sure that this could have been handled differently but all my tests failed to work. So the confirmation that this is the only solution at least will keep me from pursuing a different solution.
#4

[eluser]tomcode[/eluser]
You can surely rebuild this in Active Record, but why make out of an elegant, easy to read one-liner a three or more line statement. Active record is very good with basic stuff, but the SQL statement syntax has it's advantages, too and it is faster.
#5

[eluser]boony[/eluser]
Ha Ha Ha! When you put it like that it makes sense. Actually, I also like my solution but I think I was trying to set this up with active record for no other real reason than just to see how it could be done, which I couldn't do or get to work hence my original post. As I'm still on the CI learning curve I like to test these things to strengthen my knowledge. Ho Hum :coolsmile:
#6

[eluser]Jondolar[/eluser]
One reason for using Active Record is to abstract the actual SQL syntax. If it could ever be possible that your syntax would not work on other databases and there is a chance that your script will use a different db engine then using Active Record would be a better choice IMO.

Plus, I'd like to see the syntax myself Smile




Theme © iAndrew 2016 - Forum software by © MyBB