MYSQL: select row where field is smaller than other - mysql

this is my mysql query in php:
mysql_query("SELECT * FROM tbl WHERE logins < limit")
the mysql_affected_rows() returns -1. why this simple query doesnt work? field and table names are correct i've double checked

Try quoting your column names with backticks:
mysql_query("SELECT * FROM tbl WHERE `logins` < `limit`")
You've got a column name same as a reserved MySQL keyword (limit).

You can't use mysql_affected_rows for SELECT queries, as per the PHP Manual:
mysql_affected_rows
Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query
Use mysql_num_rows instead.
Also, as p.campbell pointed out, don't forget to backquote the name of your 'limit' column, since you use a reserved work as the column name.

Related

mysql SELECT WHERE LIKE multiple conditions

Nothing is being returned when I execute this query!
I'm trying to have multiple values for same column
SELECT * FROM `users` where Number='1212' AND Number='0921'
please, can you help?
Change the 'AND' to an 'OR'. A number (or anything else in a table column for that matter) cannot be both values at the same time
SELECT * FROM `users` where Number='1212' OR Number='0921'
Where you need rows where an element can be one of a number of values, it is common to use IN instead of repeated OR clauses, as this makes the expression clearer:
SELECT * FROM `users` where Number IN ('1212','0921');
As one of your Number values has a leading zero, I presume it is genuinely a string. You might want to name your columns better.
Or better use the IN operator, so you could filter for even more values:
SELECT * FROM `users` where Number IN ('1212', '0921');
Try this:
SELECT * FROM users where Number=1212 OR Number=0921
Change AND by OR, if you need to check equality with more values you could just use SELECT * FROM users WHERE number IN(1212, 0921, 1452, 1265);

simple sql query returns #1064 - You have an error in your SQL syntax

I've triple checked my syntax but I am completely stumped on this. All I'm doing is a simple SELECT WHERE mysql query but it's returning #1064 error. It has something to do with the second value check because when I remove it it works fine. Here's my query:
SELECT * FROM flo_chart WHERE im_key='357803040539808' AND key='b8cb8ebc11dbb641e2290c7ff954d6f8'
What is wrong with my key string that makes this query fail?!?!?!?!
Cheers!
I used a reserved word for a field name so when it came to the sql query it failed
It is not possible to use the keyword KEY as a column name. Try using some other names. (Ofcourse alter the column name)
SELECT * FROM flo_chart WHERE im_key='357803040539808' AND
key_New='b8cb8ebc11dbb641e2290c7ff954d6f8
SELECT *
FROM some_table
WHERE im_key='357803040539808'
AND ym_key='b8cb8ebc11dbb641e2290c7ff954d6f8';
EDIT
After you posted up your actual query
SELECT * FROM flo_chart WHERE im_key='357803040539808' AND key='b8cb8ebc11dbb641e2290c7ff954d6f8'
you have used a reserved word key
There are some reserved key words in mysql, in order to use that reserved word you need to and use `backtick` to wrap the keyword.
key is reserved word in mysql
Solution:
SELECT * FROM flo_chart WHERE im_key='357803040539808' AND `key`='b8cb8ebc11dbb641e2290c7ff954d6f8'
^^^^^

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

SELECT in mysql using column number instead of name

Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";

MySQL Unexpected Result from "in (' ' or ' ')"

What I'm Using: The most recent MySQL on Ubuntu 12.
The Set Up: Suppose I have a table "EmployeePayment" with "Name" and "Hours" for each employee. Suppose I already have it populated with values.
The Question: When I use the command
select * from EmployeePayment where Name in ('');
I get the empty set, as I'd expect. But, when I use
select * from EmployeePayment where Name in ('' or '');
I get the entire table returned. Moreover, if I'm picky and put in the command
select Name, SUM(Hours) from EmployeePayment where Name in ('' or '');
then it only returns whatever is the top name from the table. What's happening with this "in" command?
First off, you need to get rid of the or, the proper syntax for the in clause uses commas to separate the possibilities, such as:
sql> select name from people where status in ('intelligent', 'good looking')
pax
1 row returned
What your current variant is doing is applying the or operator to give you a one-element in-list. See here for more detail.
The reason why you're only getting one row for the aggregated query is because you have no group by clause, so you're grouping all rows. Most DBMS' would then complain about having a non-aggregated column that isn't part of the grouping, but MySQL is a bit fancy-free and footloose with the rules in that regard.
It's obviously grouping over the whole table (as it should) but applying some default aggregating function to the name (which it probably shouldn't, but does according to its documentation).
This MySQL extension is covered here but heed the warning: MySQL can choose any of the myriad possible values for these non-aggregated, non-group-by columns, so it's more useful when you know that all the rows in a given group share the same value for the column.
You're effectively doing this:
select * from EmployeePayment where Name in (0);
The OR expression evaluates to 0, and WHERE Name IN (0); returns all rows. You have to use the proper IN syntax as suggested in the other answers:
SELECT * FROM EmployeePayment WHERE Name IN ('foo', 'bar');
IN uses comma separated values, for example: WHERE Name IN ('tim','beth')
So try WHERE Name IN ('','');
But more importantly, why would you want to check where a value is empty or empty? Or was that just to get the question across?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in