简体   繁体   中英

Joining two tables in MySQL

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_configuration you are using in your WHERE clause. Do that and it should work with the clause you wrote.

So you'd end up with something like:

 SELECT * FROM all_configurator_power_text
 INNER JOIN all_configurator_lasers_power 
 ON all_configurator_lasers_power.laser_configuration = all_configurator_power_text.laser_configuration
 WHERE all_configurator_lasers_power.laser_configuration = 1

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:

 SELECT * FROM all_configurator_power_text text
 INNER JOIN all_configurator_lasers_power lasers
 ON lasers.laser_configuration = text.laser_configuration
 WHERE lasers.laser_configuration = 1

尝试添加以下内容:

WHERE all_configurator_lasers_power.power_id = 1

There are some potential issues with your table structure but without knowing more about your specific requirements it is hard to tell. For instance you have have power_id and laser_configuration in each table.

The probable issue with your where clause is that you need to specify which of the two table's laser_configuration you are using. ie:

WHERE all_configurator_lasers_power.power_id = 1
SELECT *
FROM all_configurator_lasers_power
INNER JOIN all_configurator_power_text
ON all_configurator_lasers_power.laser_id = all_configurator_power_text.laser_configuration
WHERE all_configurator_lasers_power.laser_id = $id (in your case 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM