How to inner join from another table and order by specific row value?

JWDev

In WordPress, there are 2 tables: wp_terms and wp_termmeta.

I currently do the following to get the terms and parents etc:

$query = mysqli_query($mysqli, "
SELECT wp_terms.term_id,wp_terms.name, wp_term_taxonomy.parent
FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'post-categories'
ORDER BY name"); 

However - I need to access the wp_termmeta table, select a row where the term_id is the same and the meta_key is "order" then order by the meta_value of the wp_termmeta, instead of name as it is currently.

How would I do that with the current query?

Tobias Xy

I don't have wordpress-specific knowledge, but according to your description the query could look like this (I added table aliases for readability, but they are not necessary):

SELECT t.term_id, t.name, tax.parent
FROM wp_terms t
INNER JOIN wp_term_taxonomy tax ON t.term_id = tax.term_id
INNER JOIN wp_termmeta m ON m.term_id = t.term_id AND m.meta_key = 'order'
WHERE tax.taxonomy = 'post-categories'
ORDER BY m.meta_value

The most interesting part here is the join condition of the second join, which contains two conditions: m.term_id = t.term_id AND m.meta_key = 'order' It first makes sure the term ids are matching and then assures that the meta key of the joined row is actually "order".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

adding INNER JOIN to convert ID to a value from another table

From Dev

Inner join with one row of another table

From Dev

How to get latest value from table with self inner join

From Dev

How to join a specific table row?

From Dev

How to inner join a table with the pseudo value

From Dev

Row value from another table

From Dev

How to add a value from one table to a particular row in another table?

From Dev

How to access a row on a table based on a value from another table?

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

How to choose a value from another table using a JOIN

From Dev

How to join from another table based on the highest value

From Dev

Inner join returning more than 1 row from second table

From Dev

How to get matching data from another SQL table for two different columns: Inner Join and/or Union?

From Dev

UPDATE on inner join with another table

From Dev

How to reference a table's "Total Row" value from another sheet?

From Dev

Update a specific row with data from another table

From Dev

Compute table name to join from row value

From Dev

How to inner join table?

From Dev

SQL inner join on a column based on max value from another column

From Dev

How to join one select with another when the first one not always returns a value for specific row?

From Dev

How to move Specific row in a table to another table when that row is updated

From Dev

How to get DISTINCT row from INNER JOIN Query in SQL Server

From Dev

How to get DISTINCT row from INNER JOIN Query in SQL Server

From Dev

Select the minimum value for each row join by another table

From Dev

How to join a table to itself and get max row then connect another table to it

From Dev

order by in inner join while updating table

From Dev

order by in inner join while updating table

From Dev

copy specific row from multiple table into another table in sql

From Dev

Joining row from one table with the sum value from another table

Related Related

  1. 1

    adding INNER JOIN to convert ID to a value from another table

  2. 2

    Inner join with one row of another table

  3. 3

    How to get latest value from table with self inner join

  4. 4

    How to join a specific table row?

  5. 5

    How to inner join a table with the pseudo value

  6. 6

    Row value from another table

  7. 7

    How to add a value from one table to a particular row in another table?

  8. 8

    How to access a row on a table based on a value from another table?

  9. 9

    How to sum a specific row from one table to another in SQL Server

  10. 10

    How to choose a value from another table using a JOIN

  11. 11

    How to join from another table based on the highest value

  12. 12

    Inner join returning more than 1 row from second table

  13. 13

    How to get matching data from another SQL table for two different columns: Inner Join and/or Union?

  14. 14

    UPDATE on inner join with another table

  15. 15

    How to reference a table's "Total Row" value from another sheet?

  16. 16

    Update a specific row with data from another table

  17. 17

    Compute table name to join from row value

  18. 18

    How to inner join table?

  19. 19

    SQL inner join on a column based on max value from another column

  20. 20

    How to join one select with another when the first one not always returns a value for specific row?

  21. 21

    How to move Specific row in a table to another table when that row is updated

  22. 22

    How to get DISTINCT row from INNER JOIN Query in SQL Server

  23. 23

    How to get DISTINCT row from INNER JOIN Query in SQL Server

  24. 24

    Select the minimum value for each row join by another table

  25. 25

    How to join a table to itself and get max row then connect another table to it

  26. 26

    order by in inner join while updating table

  27. 27

    order by in inner join while updating table

  28. 28

    copy specific row from multiple table into another table in sql

  29. 29

    Joining row from one table with the sum value from another table

HotTag

Archive