Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:59:59+00:00 2026-05-30T17:59:59+00:00

I’ve been relying on CREATE VIEW and aliasing a lot to create SQL queries,

  • 0

I’ve been relying on CREATE VIEW and aliasing a lot to create SQL queries, and am sure there’s probably a much more efficient way to go about it. Can anyone offer any general advice? Here’s my latest abomination:

SELECT associations2.object_id, associations2.term_id, associations2.cat_ID, associations2.term_taxonomy_id
FROM (SELECT objects_tags.object_id, objects_tags.term_id, wp_cb_tags2cats.cat_ID, categories.term_taxonomy_id
    FROM (SELECT wp_term_relationships.object_id, wp_term_taxonomy.term_id, wp_term_taxonomy.term_taxonomy_id
        FROM wp_term_relationships
        LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        ORDER BY object_id ASC, term_id ASC) 
        AS objects_tags
    LEFT JOIN wp_cb_tags2cats ON objects_tags.term_id = wp_cb_tags2cats.tag_ID
    LEFT JOIN (SELECT wp_term_relationships.object_id, wp_term_taxonomy.term_id as cat_ID, wp_term_taxonomy.term_taxonomy_id
        FROM wp_term_relationships
        LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        WHERE wp_term_taxonomy.taxonomy = 'category'
        GROUP BY object_id, cat_ID, term_taxonomy_id
        ORDER BY object_id, cat_ID, term_taxonomy_id) 
        AS categories on wp_cb_tags2cats.cat_ID = categories.term_id
    WHERE objects_tags.term_id = wp_cb_tags2cats.tag_ID
    GROUP BY object_id, term_id, cat_ID, term_taxonomy_id
    ORDER BY object_id ASC, term_id ASC, cat_ID ASC) 
    AS associations2
LEFT JOIN categories ON associations2.object_id = categories.object_id
WHERE associations2.cat_ID <> categories.cat_ID
GROUP BY object_id, term_id, cat_ID, term_taxonomy_id
ORDER BY object_id, term_id, cat_ID, term_taxonomy_id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:00:00+00:00Added an answer on May 30, 2026 at 6:00 pm

    After doing some research into different types of joins and better ways to use aliasing, my SQL code is much more condensed and efficient (doesn’t take as long to run). The answer was two-fold: use the proper JOIN method to reduce the number of rows returned – INNER JOIN was particularly helpful in most cases (see http://www.w3schools.com/sql/sql_join_inner.asp) – and use aliasing to join the same table on multiple columns.

    The latter solution was particularly helpful, because what I was doing before was essentially creating four or five views (using CREATE VIEW) in order to return data and then filter that data based on other criteria. For example, I was using a “CREATE VIEW view1 AS SELECT” statement to return rows a, b, c, and d, and then using a “SELECT a FROM view1 WHERE criterion” statement to filter the data. Sometimes this was repeated multiple times.

    A more efficient way to accomplish the same task was to use multiple JOINs on the same table using aliasing. Combined with INNER JOIN for most queries, it substantially reduced query time and returned the same rows as the more complex statements.

    Example:

    SELECT * 
    FROM table1
    INNER JOIN table2 AS alias1
      ON table1.col1 = alias1.col1
    INNER JOIN table2 AS alias2
      ON table1.col2 = alias2.col2
    WHERE col2 = 'some_condition'
    

    This method has been a life saver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
In my XML file chapters tag has more chapter tag.i need to display chapters
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.