Here are my two tables. I’m not sure they’re normalized correctly:
all_configurator_lasers_power:
laser_id | power_id | laser_configuration
=========================================
1 10 1
1 25 1
1 20 2
1 50 2
2 10 1 ...
all_configurator_power_text:
power_id | power_level | laser_configuration | power_text
=========================================================
10 10 watts 1 10 watt text
25 25 watts 1 25 watt text
20 20 watts 2 20 watt text
50 50 watts 2 50 watt text
What I want back is the first two rows of the second table if I provide the laser_id in the first table.
This is what I tried (but didn’t work)
'SELECT * FROM all_configurator_power_text
INNER JOIN all_configurator_power_text
ON all_configurator_lasers_power.laser_configuration = all_configurator_power_text.laser_configuration'
This returns an array like this:
Array (
[0] => stdClass Object
(
[id] => 1
[language] => en
[power_id] => 10
[power_level] => 10 watts
[laser_configuration] => 1
[power_text] => 10 watt text
[laser_id] => 1
)
...)
But the array (a CodeIgniter object) also returns objects with a laser_configuration of 2. I want just the ones with a 1. I added a WHERE clause of WHERE laser_configuration = 1 but then I get a non-object error.
What am I doing wrong?
You need to specify which table’s
laser_configurationyou are using in yourWHEREclause. Do that and it should work with the clause you wrote.So you’d end up with something like:
Also, I recommend adding some shorthand references to make that more readable. It could play a big part in keeping yourself sane over the years: