简体   繁体   中英

MySQL Select statement with where condition and inner join

I am using Geodesic Solutions. I need help with a query since I'm making an addon. The tables in my database that I need to query are

geodesic_leveled_field_value_languages
 -id
 -language_id
 -name

geodesic_leveled_field_value
 -id
 -leveled_id
 -parent
 -leveled_field
 -enabled
 -display_order

My Sql Statement is

$sqlSelect = "SELECT geodesic_leveled_field_value_languages.name AS \"FieldName\", geodesic_leveled_field_value.leveled_field AS \"FieldLevel\" FROM `geodesic_leveled_field_value` INNER JOIN `geodesic_leveled_field_value_languages` on geodesic_leveled_field_value.id = geodesic_leveled_field_value_languages.id";

I want it to return only the Names (from geodesic_leveled_field_value_languages table) for a specific leveled_value (in geodesic_leveled_field_value table)

I've also tried the following query but no luck

 $sqlSelect = "SELECT geodesic_leveled_field_value_languages.name AS \"FieldName\", geodesic_leveled_field_value.leveled_field AS \"FieldLevel\" FROM `geodesic_leveled_field_value` where geodesic_leveled_field_value.leveled_field = 2 INNER JOIN `geodesic_leveled_field_value_languages` on geodesic_leveled_field_value.id = geodesic_leveled_field_value_languages.id";

The sql will look something like this:

SELECT 
    gl.name
FROM
    geodesic_leveled_field_value_languages gl 
    INNER JOIN geodesic_leveled_field_value gv ON gv.id = gl.id
WHERE
    gv.level='required level value'

NOTE: This sql assumes that the level column is the column you want to filter data with and also assumes that both tables are connected with the id fileds.

You were missing the select clause where you specify each column from which table you want to return and you were missing the where condition that specifies what to return based in a filter criteria

You can learn more about sql.where clause in the following link https://www.w3schools.com/sql/sql_where.asp

Note that gl and gv are aliases. You can find out more about aliases on the url

https://www.w3schools.com/sql/sql_alias.asp

This has nothing to do with tying

You know your database and should know which fields build a relationship.

Further you can also use aliases for the tables, that will in future save you some time.

Also you can use between two double quotes a single quote or vice versa

the field leveled_field in yr query is not in any of the two tables, so i am gusssing you meant the level

$sqlSelect = "SELECT gvl.name AS 'FieldName', gv.level AS 'FieldLevel' FROM `geodesic_leveled_field_value`  gv INNER JOIN `geodesic_leveled_field_value_languages` gvl on gv.id = gvl.id WHERE gv.level > 5;"

I solved my problem with the following query,

$sqlSelect = "SELECT geodesic_leveled_field_value_languages.name FROM `geodesic_leveled_field_value` INNER JOIN `geodesic_leveled_field_value_languages` on geodesic_leveled_field_value.id = geodesic_leveled_field_value_languages.id and geodesic_leveled_field_value.leveled_field = 2";

Basically I just added an AND statement after ON clause. It selects the data from the two tables based on both the ON clauses.

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