How to get data between two range from mysql with complex string





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Employee table



Id  Name Salary frame
1 A 5000 MDF-125NH
2 b 10000 MDF-025AH
3 c 15000 MDF-325KH
4 d 20000 MDF-425LH
5 e 25000 MDF-521MH


I want to get data between to Frame i.e(MDF-125NH, MDF-325KH) from MySQL.



I try something but no success.



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) AND (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) )


OR



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING("MDF-125NH", 0, CHARINDEX('-', "MDF-125NH"))
FROM employee
) AND (
SELECT SUBSTRING("MDF-325KH", 0, CHARINDEX('-', "MDF-325KH"))
FROM employee
) )









share|improve this question

























  • In what sense is something 'between' one frame and another frame

    – Strawberry
    Nov 21 '18 at 19:13











  • Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

    – Used_By_Already
    Nov 22 '18 at 4:30


















0















Employee table



Id  Name Salary frame
1 A 5000 MDF-125NH
2 b 10000 MDF-025AH
3 c 15000 MDF-325KH
4 d 20000 MDF-425LH
5 e 25000 MDF-521MH


I want to get data between to Frame i.e(MDF-125NH, MDF-325KH) from MySQL.



I try something but no success.



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) AND (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) )


OR



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING("MDF-125NH", 0, CHARINDEX('-', "MDF-125NH"))
FROM employee
) AND (
SELECT SUBSTRING("MDF-325KH", 0, CHARINDEX('-', "MDF-325KH"))
FROM employee
) )









share|improve this question

























  • In what sense is something 'between' one frame and another frame

    – Strawberry
    Nov 21 '18 at 19:13











  • Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

    – Used_By_Already
    Nov 22 '18 at 4:30














0












0








0








Employee table



Id  Name Salary frame
1 A 5000 MDF-125NH
2 b 10000 MDF-025AH
3 c 15000 MDF-325KH
4 d 20000 MDF-425LH
5 e 25000 MDF-521MH


I want to get data between to Frame i.e(MDF-125NH, MDF-325KH) from MySQL.



I try something but no success.



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) AND (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) )


OR



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING("MDF-125NH", 0, CHARINDEX('-', "MDF-125NH"))
FROM employee
) AND (
SELECT SUBSTRING("MDF-325KH", 0, CHARINDEX('-', "MDF-325KH"))
FROM employee
) )









share|improve this question
















Employee table



Id  Name Salary frame
1 A 5000 MDF-125NH
2 b 10000 MDF-025AH
3 c 15000 MDF-325KH
4 d 20000 MDF-425LH
5 e 25000 MDF-521MH


I want to get data between to Frame i.e(MDF-125NH, MDF-325KH) from MySQL.



I try something but no success.



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) AND (
SELECT SUBSTRING(frame, 0, CHARINDEX('-', frame))
FROM employee
) )


OR



SELECT DISTINCT (id) AS ln
FROM employee c04 BETWEEN (
SELECT SUBSTRING("MDF-125NH", 0, CHARINDEX('-', "MDF-125NH"))
FROM employee
) AND (
SELECT SUBSTRING("MDF-325KH", 0, CHARINDEX('-', "MDF-325KH"))
FROM employee
) )






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 4:28









Used_By_Already

23.2k22139




23.2k22139










asked Nov 21 '18 at 16:54









Abid HussainAbid Hussain

6,79122445




6,79122445













  • In what sense is something 'between' one frame and another frame

    – Strawberry
    Nov 21 '18 at 19:13











  • Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

    – Used_By_Already
    Nov 22 '18 at 4:30



















  • In what sense is something 'between' one frame and another frame

    – Strawberry
    Nov 21 '18 at 19:13











  • Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

    – Used_By_Already
    Nov 22 '18 at 4:30

















In what sense is something 'between' one frame and another frame

– Strawberry
Nov 21 '18 at 19:13





In what sense is something 'between' one frame and another frame

– Strawberry
Nov 21 '18 at 19:13













Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

– Used_By_Already
Nov 22 '18 at 4:30





Neither of those queries are valid syntax. Plus you need to specify what you mean as a "frame" which is something you understand, but I know nothing about.

– Used_By_Already
Nov 22 '18 at 4:30












1 Answer
1






active

oldest

votes


















0














CREATE TABLE employee(
Id INTEGER NOT NULL PRIMARY KEY
,Name VARCHAR(1) NOT NULL
,Salary INTEGER NOT NULL
,frame VARCHAR(9) NOT NULL
);
INSERT INTO employee(Id,Name,Salary,frame) VALUES (1,'A',5000,'MDF-125NH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (2,'b',10000,'MDF-025AH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (3,'c',15000,'MDF-325KH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (4,'d',20000,'MDF-425LH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (5,'e',25000,'MDF-521MH');

SELECT *
FROM employee
WHERE frame BETWEEN 'MDF-125NH' and 'MDF-325KH'
;


This results is:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


'between' in SQL has a very specific meaning which is the equivalent of:



frame >= 'MDF-125NH' and frame <= 'MDF-325KH'


This definition may not necessarily align with what you expect.



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH | << is this the "between" you want?
| 3 | 3 | c | 15000 | MDF-325KH |
| 4 | 4 | d | 20000 | MDF-425LH |
| 5 | 5 | e | 25000 | MDF-521MH |
+----+----+------+--------+-----------+


This query:



SELECT *
FROM employee
WHERE id BETWEEN (select min(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
AND (select max(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
;


produces this result:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH |
| 3 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


see this demonstrated






share|improve this answer


























  • see: rextester.com/LOYM91730

    – Used_By_Already
    Nov 22 '18 at 4:42












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417007%2fhow-to-get-data-between-two-range-from-mysql-with-complex-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














CREATE TABLE employee(
Id INTEGER NOT NULL PRIMARY KEY
,Name VARCHAR(1) NOT NULL
,Salary INTEGER NOT NULL
,frame VARCHAR(9) NOT NULL
);
INSERT INTO employee(Id,Name,Salary,frame) VALUES (1,'A',5000,'MDF-125NH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (2,'b',10000,'MDF-025AH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (3,'c',15000,'MDF-325KH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (4,'d',20000,'MDF-425LH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (5,'e',25000,'MDF-521MH');

SELECT *
FROM employee
WHERE frame BETWEEN 'MDF-125NH' and 'MDF-325KH'
;


This results is:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


'between' in SQL has a very specific meaning which is the equivalent of:



frame >= 'MDF-125NH' and frame <= 'MDF-325KH'


This definition may not necessarily align with what you expect.



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH | << is this the "between" you want?
| 3 | 3 | c | 15000 | MDF-325KH |
| 4 | 4 | d | 20000 | MDF-425LH |
| 5 | 5 | e | 25000 | MDF-521MH |
+----+----+------+--------+-----------+


This query:



SELECT *
FROM employee
WHERE id BETWEEN (select min(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
AND (select max(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
;


produces this result:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH |
| 3 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


see this demonstrated






share|improve this answer


























  • see: rextester.com/LOYM91730

    – Used_By_Already
    Nov 22 '18 at 4:42
















0














CREATE TABLE employee(
Id INTEGER NOT NULL PRIMARY KEY
,Name VARCHAR(1) NOT NULL
,Salary INTEGER NOT NULL
,frame VARCHAR(9) NOT NULL
);
INSERT INTO employee(Id,Name,Salary,frame) VALUES (1,'A',5000,'MDF-125NH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (2,'b',10000,'MDF-025AH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (3,'c',15000,'MDF-325KH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (4,'d',20000,'MDF-425LH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (5,'e',25000,'MDF-521MH');

SELECT *
FROM employee
WHERE frame BETWEEN 'MDF-125NH' and 'MDF-325KH'
;


This results is:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


'between' in SQL has a very specific meaning which is the equivalent of:



frame >= 'MDF-125NH' and frame <= 'MDF-325KH'


This definition may not necessarily align with what you expect.



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH | << is this the "between" you want?
| 3 | 3 | c | 15000 | MDF-325KH |
| 4 | 4 | d | 20000 | MDF-425LH |
| 5 | 5 | e | 25000 | MDF-521MH |
+----+----+------+--------+-----------+


This query:



SELECT *
FROM employee
WHERE id BETWEEN (select min(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
AND (select max(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
;


produces this result:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH |
| 3 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


see this demonstrated






share|improve this answer


























  • see: rextester.com/LOYM91730

    – Used_By_Already
    Nov 22 '18 at 4:42














0












0








0







CREATE TABLE employee(
Id INTEGER NOT NULL PRIMARY KEY
,Name VARCHAR(1) NOT NULL
,Salary INTEGER NOT NULL
,frame VARCHAR(9) NOT NULL
);
INSERT INTO employee(Id,Name,Salary,frame) VALUES (1,'A',5000,'MDF-125NH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (2,'b',10000,'MDF-025AH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (3,'c',15000,'MDF-325KH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (4,'d',20000,'MDF-425LH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (5,'e',25000,'MDF-521MH');

SELECT *
FROM employee
WHERE frame BETWEEN 'MDF-125NH' and 'MDF-325KH'
;


This results is:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


'between' in SQL has a very specific meaning which is the equivalent of:



frame >= 'MDF-125NH' and frame <= 'MDF-325KH'


This definition may not necessarily align with what you expect.



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH | << is this the "between" you want?
| 3 | 3 | c | 15000 | MDF-325KH |
| 4 | 4 | d | 20000 | MDF-425LH |
| 5 | 5 | e | 25000 | MDF-521MH |
+----+----+------+--------+-----------+


This query:



SELECT *
FROM employee
WHERE id BETWEEN (select min(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
AND (select max(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
;


produces this result:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH |
| 3 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


see this demonstrated






share|improve this answer















CREATE TABLE employee(
Id INTEGER NOT NULL PRIMARY KEY
,Name VARCHAR(1) NOT NULL
,Salary INTEGER NOT NULL
,frame VARCHAR(9) NOT NULL
);
INSERT INTO employee(Id,Name,Salary,frame) VALUES (1,'A',5000,'MDF-125NH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (2,'b',10000,'MDF-025AH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (3,'c',15000,'MDF-325KH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (4,'d',20000,'MDF-425LH');
INSERT INTO employee(Id,Name,Salary,frame) VALUES (5,'e',25000,'MDF-521MH');

SELECT *
FROM employee
WHERE frame BETWEEN 'MDF-125NH' and 'MDF-325KH'
;


This results is:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


'between' in SQL has a very specific meaning which is the equivalent of:



frame >= 'MDF-125NH' and frame <= 'MDF-325KH'


This definition may not necessarily align with what you expect.



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH | << is this the "between" you want?
| 3 | 3 | c | 15000 | MDF-325KH |
| 4 | 4 | d | 20000 | MDF-425LH |
| 5 | 5 | e | 25000 | MDF-521MH |
+----+----+------+--------+-----------+


This query:



SELECT *
FROM employee
WHERE id BETWEEN (select min(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
AND (select max(id) from employee where frame IN ('MDF-125NH','MDF-325KH'))
;


produces this result:



+----+----+------+--------+-----------+
| | Id | Name | Salary | frame |
+----+----+------+--------+-----------+
| 1 | 1 | A | 5000 | MDF-125NH |
| 2 | 2 | b | 10000 | MDF-025AH |
| 3 | 3 | c | 15000 | MDF-325KH |
+----+----+------+--------+-----------+


see this demonstrated







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 4:47

























answered Nov 22 '18 at 4:41









Used_By_AlreadyUsed_By_Already

23.2k22139




23.2k22139













  • see: rextester.com/LOYM91730

    – Used_By_Already
    Nov 22 '18 at 4:42



















  • see: rextester.com/LOYM91730

    – Used_By_Already
    Nov 22 '18 at 4:42

















see: rextester.com/LOYM91730

– Used_By_Already
Nov 22 '18 at 4:42





see: rextester.com/LOYM91730

– Used_By_Already
Nov 22 '18 at 4:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417007%2fhow-to-get-data-between-two-range-from-mysql-with-complex-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)