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 8403051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:14:29+00:00 2026-06-09T22:14:29+00:00

I want to get the SELECT block of an SQL query, and what I’m

  • 0

I want to get the SELECT block of an SQL query, and what I’m thinking is getting the text between the words SELECT and FROM. What is the REGEX I’m supposed to use?

Is it possible to extract the FROM, WHERE and JOIN blocks in the same manner?

UPDATE

I cannot use the http://code.google.com/p/php-sql-parser/ because it is built for MySQL and my MSSQL queries break it. I’ve searched a lot for an MSSQL parser and couldn’t find a ready made solution so the only way I can think of is the parse the SQL myself using regex and work on the principle that the select block is between a SELECT and FROM keyword.

EXAMPLE

For the query

SELECT STRAIGHT_JOIN a,b,c 
FROM some_table an_alias
WHERE d > 5;

I want something like this to be returned:

$result['SELECT'] => 'STRAIGHT_JOIN a,b,c';
$result['FROM'] => 'some_table an_alias';
$result['WHERE'] => 'd>5';
  • 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-06-09T22:14:31+00:00Added an answer on June 9, 2026 at 10:14 pm

    You could use a SQL parser library to do that for you.

    Checkout this project http://code.google.com/p/php-sql-parser/

    Sample input:

    SELECT STRAIGHT_JOIN a,b,c 
      from some_table an_alias
     WHERE d > 5;
    

    Sample output:

    Array
    ( 
    [OPTIONS] => Array
        (
            [0] => STRAIGHT_JOIN
        )       
    
    [SELECT] => Array
        (
            [0] => Array
                (
                    [expr_type] => colref
                    [base_expr] => a
                    [sub_tree] => 
                    [alias] => `a`
                )
    
            [1] => Array
                (
                    [expr_type] => colref
                    [base_expr] => b
                    [sub_tree] => 
                    [alias] => `b`
                )
    
            [2] => Array
                (
                    [expr_type] => colref
                    [base_expr] => c
                    [sub_tree] => 
                    [alias] => `c`
                )
    
        )
    
    [FROM] => Array
        (
            [0] => Array
                (
                    [table] => some_table
                    [alias] => an_alias
                    [join_type] => JOIN
                    [ref_type] => 
                    [ref_clause] => 
                    [base_expr] => 
                    [sub_tree] => 
                )
    
        )
    
    [WHERE] => Array
        (
            [0] => Array
                (
                    [expr_type] => colref
                    [base_expr] => d
                    [sub_tree] => 
                )
    
            [1] => Array
                (
                    [expr_type] => operator
                    [base_expr] => >
                    [sub_tree] => 
                )
    
            [2] => Array
                (
                    [expr_type] => const
                    [base_expr] => 5
                    [sub_tree] => 
                )
    
        )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get back the text that I select in an element using
I want to get HTML source of select text in div and alert that.
I am trying to select three values from xml document. I want to get
I want to get the next select element (because there will be multiple with
I want to get possibility to select several Categories for one Post with multiple
Basically I want to get the value of a textbox which is to select
I execute a select to get the structure of a table. I want to
I want get as much as possible from Redis + Hiredis + libevent. I'm
I am using the following query to return a single value: select er.elig_code from
i have a select tag , when its value changes i want to get

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.