Filter integer result from Mysql query - mysql

I have a Mysql query. I want to filter only integer result.
My query is-
SELECT * FROM table as p WHERE p.test between 0 AND 999
But result comes this-
747
748
749
FO4001
FO4002
750
751
I want to ask two things-
1)Is there any way to exclude below result-
FO4001
FO4002
2)Why are these coming in the result?

Try this one, use REGEXP to test if the value is all numeric.
SELECT *
FROM table1
WHERE x BETWEEN 0 AND 999
AND x REGEXP '^[0-9]+$';
SQLFiddle Demo

Related

Mysql subquery not working properly with comma-separated value in column

I have a MySQL table Mapdetail that has resultTypeIds column storing a comma separated list of ids:
MapDetails
=============================
mapheaderid | resultTypeIds
=============================
54 | 1,4,-9999
Now, when I try it as a subquery, it does not return me the correct result while if I hard-code (1,4,-9999) it works fine
WRONG - RETURNING 1 ROW ONLY
select * from resulttypes rt where rt.resulttypeid in
(select md1.resulttypeids from mapdetail md1
where md1.mapheaderid = 54)
CORRECT - RETURNING 2 ROWS
select * from resulttypes rt where rt.resulttypeid in (1,4,-9999)
If you can't re-structure your DB (as mentioned in Comments). Try using FIND_IN_SET instead of IN
Try something like:
select * from resulttypes rt where FIND_IN_SET (rt.resulttypeid,
(select md1.resulttypeids from mapdetail md1
where md1.mapheaderid = 54))

Mysql Query to display records as it is using IN() in where clause and without any sorting

I need mysql query to display the order when i have subquery in it without any sorting
Query which i am using
SELECT * FROM attributes WHERE AttributesID IN (SELECT AttributesID
FROM processattributes WHERE ProcessID = 166)
result i am getting as below
ID NAME
218 AA-Delays
219 AA-Internal
220 AA-External Errors
Am supposed to GET as below
ID Name
219 AA-External Errors
220 AA-Internal
218 AA-Delays
As sub query returns correctly 219 220 218 but final is not correctly please help out in the query
The inner select does not return the data in a garantied way. So you have to define the order you want. You can join and then define the order like this (but I don't know on which column in the other table you want to sort)
SELECT a.*
FROM attributes a
JOIN processattributes p on p.AttributesID = a.AttributesID
WHERE p.ProcessID = 166
ORDER BY <some column in table processattributes>

How to define Query within LIKE statement

I use this query to match data from two tables but it returns
Error Code: 1242
Subquery returns more than 1 row
my example tables and query are below.
SELECT
ACCOUNT_CODE,
DR_AMOUNT,
CR_AMOUNT
FROM
`tblinvoices`
WHERE ACCOUNT_CODE LIKE
CONCAT((
SELECT
LEFT(SUB_GROUP, 4)
FROM
`tblcharts`
WHERE ACC_TYPE = 'G'), '%') ;
Temp Table.
SUB_GROUP
------------------
1-01
2-01
3-01
4-01
6-01
6-02
6-03
7-01
7-02
8-01
9-01
DATA TABLE TBLINVOICES
ACCOUNT_CODE DR_AMOUNT CR_AMOUNT
------------ --------- -----------
6-03-0001 27500 0
6-02-0001 0 27500
6-03-0001 1700 0
6-02-0001 0 1700
3-01-0005 15000 0
6-03-0001 0 15000
6-03-0001 315432 0
6-02-0002 0 315432
I want to get all debit and credit amounts where match first 4 character of group code with invoice account code. Please anyone help in this regards.
I think you want an aggregation query. It would look something like this:
SELECT LEFT(i.ACCOUNT_CODE, 4) as sub_group, SUM(i.DR_AMOUNT) as DR_AMOUNT,
SUM(i.CR_AMOUNT) as CR_AMOUNT
FROM tblinvoices i
GROUP BY LEFT(i.ACCOUNT_CODE, 4);
I believe you can do what you want, using a subquery in the like clause BUT you must be sure that the subquery returns only one result: does not make sense to database to compare a single line of tblinvoices to a lot of lines.
You can achieve what you want narrowing the results in subquery's where clause. Also, as long as you comparing just a fraction of the whole field, I believe you should concat '%' characters at the end of the result of the subquery,or it will never match!
Put the data/format of tblcharts so I can help in a more detailed way.

Calculate total of some fields in mysql?

I have a table like this
item_id item_quantity
----------------------
324 2
432 23
543 12
879 3
The item_id field is not auto-increment but it is unique.
The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.
For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)
Is it possible to do this at mysql level?
Try using in :
SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)
This works on MSSQL, not sure about MySQL, though:
SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)
EDIT: OK, others got the same answer. So I guess it also works on MySQL.
If all you need is the sum of the item_quantity, all you need to do is:
Select Sum(item_quantity)
From Table
Where item_id In (324, 543)

How to return all matches in mysql where value occurs in string at position n?

Im looking to run a query in phpmyadmin (mysql) that wil check a table for a specific value at a specific postion in a string, i'm new to mysql and this is what i've tried but there's a syntax issue. I'm looking to find the value "1" at position 5 and display all those users that possess this.
SELECT*
FROM`user`
WHERE`options`LOCATE(`options`,1,5)
LIMIT 0 , 30
regards,
Silo
Locate is for finding a value ANYWHERE in a string. You want a specific location only, so use substr() instead:
SELECT *
FROM user
WHERE substr(options, 5, 1) = '1'
You could try
SELECT*
FROM`user`
WHERE options LIKE '____1%'
LIMIT 0 , 30
Another ;)
SELECT *
FROM user
WHERE instr(options, '1') = 5
Duh .. Well won't work since it only returns the first occurnace :$ But CHECK the reference,
SQLFIDDLE using Locate()
Sample data:
COL1 COL2
G11 112
G11-1 0
G11-2 2
G12-2 111
Query1:
-- to check on varchar columns
SELECT *
FROM tablex
where locate('1',col1,5)
;
Results on varchar:
COL1 COL2
G11-1 0
Query 2:
-- to check on int columns
SELECT *
FROM tablex
where locate(1,col2,2)
;
Results on int:
COL1 COL2
G11 112
G12-2 111