say if the table trap has column 'id' and rows contents as
7
8
9
10
11
12
13
14
SELECT id FROM trap
WHERE id<10 and id>12
this doesn't give any output
but if
SELECT id FROM trap
WHERE id>7 and id<14
give me the required output i.e.,
8
9
10
11
12
13
The problem is that you're using the AND operator where you should be using OR.
SELECT id FROM trap WHERE id<10 OR id>12
Obviously id cannot be both 10 AND 12 at the same time, one box can only hold one value.
Alternatively you can write the statement as:
SELECT id FROM trap WHERE NOT(id BETWEEN 10 AND 12)
The reason that SELECT id FROM trap WHERE id>7 and id<14 does work is that it is possible for a value to be BETWEEN 8 AND 13 (inclusive) at the same time.
However no way can a value ever the smaller than 10 and larger than 12 at the same time.
So if the conditions are mutually exclusive you must use OR, if the conditions do not exclude one and other then you must use AND.
You can wrap a test inside a NOT() to reverse the test, this is because AND and OR are exact opposites.
I think you're mistaken, or you've phrased the question wrong.
It's impossible for id < 10 and id > 12 to be true at the same time. That's why you aren't getting any results for your first query.
What you're looking for is:
SELECT id FROM trap WHERE id<10 OR id>12
Related
select producten_pid from prodsymp where symptomen_id = 11 and symptomen_id = 18;
I am trying the select statement above on a mysql table.
But I am not getting any results, where I should.
What am I doing wrong?
Thank you for your answers.
The idea was to create a database for problems and solutions,
where one problem can have multiple solutions and one solution can solve multiple problems.
I created this reference table 'probsol' with references to both the problems and the solutions table like below.
psid 1 2 3 4 5 6 7 8 9
pid 11 11 12 12 17 18 18 19 20
sid 18 9 18 10 10 18 9 13 13
Now I am trying to create a query to find lets say a single solution to multiple problems as below:
select sid from prodsymp where pid = 11 and pid = 12;
The results for this query should be 18, but I am getting 0 results.
Mysql is working as expected. You ask for a row where symptomen_id is at the same time 11 and 18. There can be no such row, so you don't get any result.
Maybe you wanted to use OR.
Your logic is wrong. symptomen_id can't be both at the same time. Use or.
Even better, use in like so:
select producten_pid from prodsymp where symptomen_id in (11, 18).
Edit:
Afer you edited your question, your problem became quite different.
Maybe this query can help:
SELECT
sid,
count(*) as cnt
FROM
producten_pid
where
pid in (
11,12
)
group by
sid
having
count(*) > 1
Is there a built-in function or some other way to limit the value of numbers in MySQL?
For example, let's say I have a table with the following rows:
score
=====
5
10
20
50
3
15
I'd looking for some type of query along the lines of...
SELECT NUMBER_LIMITER(score, 10) FROM ScoresTable
...which would return the following result...
score
=====
5
10
10
10
3
10
If not, I'll write a function. Just hoping there might be something built-in as performance is quite important for this project. Thanks!
SELECT LEAST(score, 10) FROM ScoresTable
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_least
I would like help with sql query code to push the consequent data in a specific column down by a row.
For example in a random table like the following,
x column y column
6 6
9 4
89 30
34 15
the results should be "pushed" down a row, meaning
x column y column
6 null or 0 (preferably)
9 6
89 4
34 30
SQL tables have no inherent concept of ordering. Hence, the concept of "next row" does not make sense.
Your example has no column that specifies the order for the rows. There is no definition of next. So, what you want to do cannot be done.
I am not aware of a simple way to do this with the way you are showing the table being formatted. If your perhaps added two consecutively numbered integer fields that provide row number and row number + 1 values, you could join the table to itself and get that information.
After taking a backup of you table:
Make a PHP function that will:
- Load all values of Y into an array
- Set Y = 0 (MYSQL UPDATE)
- load the values back from PHP array to MYSQL
In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2
Let say i want to store several dataset ie
78 94 33 22 14 55 18 10 11
44 59 69 79 39 49 29 19 39
And later on i would like to be able run queries that will determine the frequency of certain number. What would be the best way to this? What would be table structure to make a fast query.
Please be specific as you can be.
To get the counts, you can run a query such as:
SELECT value, COUNT(*) from table_of_values GROUP BY value
Placing an index on the single integer value column is pretty much all you can do to speed that up.
You could of course also just keep a table with every two-digit value and a count. You will have to pre-fill the table with zero counts for every value.
Then increment the count instead of inserting:
UPDATE table_of_values SET count = count + 1 WHERE value = (whatever)