I want to run a SQL query (or multiple queries) on database of my website in phpMyadmin in the SQL tab it says
SELECT * FROM `shell` WHERE 1
SO I typed in
SELECT * FROM `shell` WHERE 'http://samiul.ru.cx/c0de.php';
but I get an error
1054 - "Unknown column 'http://samiul.ru.cx/c0de.php' in 'where clause'
The shell should be an existing table on your phpMyadmin.
After the WHERE statement you should place your column name that you want to filter. For example your table shell contain a column name shell_name with a character data type. Your sql statement should look like:
SELECT *
FROM shell
WHERE shell_name like 'any_name';
If your not planning to filter your table you can remove the WHERE statement.
Leaving you only the main sql statement:
SELECT * FROM shell;
The general syntax for a select query with where clause is:
SELECT * FROM TABLE_NAME
WHERE COLUMN_NAME = 'PARTICULAR_COLUMN_VALUE';
Where clause is used to filter records that satisfy a particular filter criteria.
For example, You have a table say EMP_TABLE with following structure and records,
EMP_NAME | EMP_NUMBER
NISHA | 3322
GRASHIA | 3696
If you want to fetch all columns of all records that have name as 'NISHA', then your query should be:
SELECT * FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
*Note: * in the above query can also be expanded as SELECT EMP_NAME, EMP_NUMBER where you can explicitly mention all the column names
If you want to fetch only particular column say emp number of employee 'NISHA' then your query can be:
SELECT EMP_NUMBER FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
For more reference on Syntax of WHERE CLAUSE
SELECT * FROM *table* WHERE *column* = *value*
In your case:
SELECT * FROM shell WHERE webpage = 'http://samiul.ru.cx/c0de.php'
Or similar.
The WHERE 1 part is a little confusing.
SELECT * FROM table WHERE 1
is functionally the same as
SELECT * FROM table
Here's an explanation: Importance of WHERE 1 in MySQL queries
I've never actually had a need for it, but maybe someone has.
This is a pretty basic question. You might want to read up a little on how queries work.
Related
I have used below mysql query for fetching data:
select *
from tableName
where tableName.field_type='22'
and tableName.field_id NOT LIKE(select aField_id
from TableName 02 where status !='Active')
I am getting error
1242 - Subquery returns more than 1 row
can you let me know what wrong in this query
select * from tableName
where tableName.field_type='22'
and
tableName.field_id
NOT IN(select aField_id from TableName where status !='Active')
Use not in in place of not like. Not in is for comparison of column with a set of values. not like is for comparison of column with a single value or pattern. Your subquery in returning more than one rows. not like can't handle that.
Like deals with only one input. So you should use IN in place of LIKE.
Is there a way to get a column name from table where all values in this column are the same!
Example! IF i would there would be a such a code it would return answer 'Works'
Table1
ID Name Works
1 Andre Yes
2 John Yes
3 Stewart Yes
I don't know if the columns of the table are known. If not, you could be able to get them by:
desc Table1;
or if you are using higher version of MySQL, you could use:
select column_name from information_schema.columns where table_schema='your_schema' and table_name='Table1';
Then you try the following statement with parameters #column being replaced with the column names retrieved from the above statement:
select count(*) from (select count(*) as c from Table1 as t group by t.#column) as sub;
If the result is 1, the column is what you want. The result means how many distinct values this column has.
I suppose you will have to use a kind of programming language or stored procedure. You are not likely being able to achieve that with one single SQL statement.
I'm not sure if it's possible to run from MySQL itself but you can do this from your scripting engine.
You can run a loop on each column and do a query:
SELECT
COUNT(DISTINCT column_name)
FROM table_name;
And see you get 1 record in the results
I want to run two mysql SELECT statements, combine them, call the new combination by its own name, then order that new combination by a user-defined function. This is what I am trying currently:
SELECT * FROM (
SELECT * FROM dictionary WHERE def1 LIKE '$input%'
UNION ALL
SELECT * FROM dictionary WHERE def2 LIKE '$input%'
) AS newcol
ORDER BY levenshtein('$input', newcol)
LIMIT 10
But I get the following error:
Unable to run query:Unknown column 'newcol' in 'order clause'
The problem is clearly with defining the new group 'newcol'.
You're trying to order by a TABLE, not by a FIELD! Use a field from your tables ant it'll go smooth
My query is like this
select 5 from mytable_name;
Then the output is like column name 5 and the value is 5 printing as many max number of rows exists in that table.
Can anybody tell the reason why this query is working like this?
Can anybody tell the reason why this query is working like this?
You are selecting a string literal value '5' for each row in your table:
select 5 from mytable_name;
And this works fine. Because in the SELECT statement you can select:
Column reference,
Literal value like in your case.
Function.
value expression.
Select expression.
As defined by the standard SQL1:
Update:
However, If you have a column with a name is a number like in your case, you have to escape it in order to select the values in it like so:
SELECT `143` FROM Table1;
This will select all the rows in the column 143.
But, this:
SELECT 143 FROM Table1;
Will select the string literal 143 for each row found in the table.
Note that: If possible, try not to name these columns this way, it is recommended and a best practice, not to do this.
SQL Fiddle Demo
Update 2:
Note that, if you select 143 or '143', or even "143" this will select the literal value 143 not the column date. The following are the same:
SELECT 143 FROM Table1;
SELECT '143' FROM Table1;
SELECT "143" FROM Table1;
All these SELECTs won't select the data in the column, They will select the literal value 143 not the column data. See this demo:
Demo
You have to escape the column name with the two:
``
Like this:
SELECT `143` FROM table1;
Not:
SELECT '143' FROM table1'
Like what I did here:
The right Demo
1Image From: SQL Queries for Mere Mortals
from mytable
will select all rows from your table if there is no where condition that shrinks that result. and
select 5
will select the constant number 5 for every record. If you use a column name in the select part then that value will be selected for every record.
The DB engine will name the result 5 because it automatically generates a column name and 5 is the logical name for that.
You want 'SELECT * FROM mytable_name LIMIT 0,5' perhaps?
Since you don't have anything in your where clause, it is selecting all the rows from your table. The fact that you don't select any of the columns is irrelevant - you'll still get a result for each row in the table.
With the command select 5 ... you are viewing a fixed value. Same thing you run the following command: select "test", you will be displaying a fixed string.
Using ... from mytable_name you're looking for all record of this table.
With this we can conclude that for each record in the table mytable_name shows you the fixed value "5".
i am trying to run a query like this
SELECT a, b , c, (SELECT INNULL(x,y)) as mycol WHERE mycol < 400 ;
BUt it gives the error
#1054 - Unknown column 'mycol' in 'where clause'
What would be the right way to do this?
Thanks.
It's so in MS SQL, so I assume, that the same problem is in MySQL.
Try to change WHERE to HAVING. WHERE clause doesn't see your renamed columns.
HAVING is working the same way as WHERE, like (mycol < 400 AND a > 5).
GROUP BY should be before HAVING.
Check the examples in the link.
http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm
#hgulyan I doubt your answer. It is not the renaming that prevents one from using WHERE clause, rather it is the subquery. So lets say I have a query:
SELECT id as ID FROM user WHERE ID > 10;
This is going to work perfectly fine.
Now lets say I have one more query:
SELECT name, (SELECT id FROM user_detail WHERE user_id = 20) as ID FROM user WHERE ID > 19;
This particular query will produce error as:
Unknown column ID
So, it's about using subquery and column aliases and not just column aliases.
Thus in this case you will have to use HAVING instead of WHERE.