MYSQL - how to select a particular column name - mysql

This is a really basic mysql question but I rarely work with mysql so here it is.
I understand the basics of selecting particular rows in a table. Here is my statement
"SELECT * FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09' "
But this returns all of the columns for the selected rows. For this particular query I only need one column 'AUCTION_ITEM', How can I return only that data for the selection above?

Replace the * by the list of your columns:
SELECT `AUCTION_ITEM` FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09'
You could have just found this in the documentation.

"SELECT DISTINCT * FROM realForeclosure WHERE auctionDate BETWEEN '2014-12-08' AND '2014-12-09' "

Related

How can I retrieve the column names from an empty MySQL select query result

Is there a way to retrieve the column names of a query that returns no data?
The result of this query would be empty.
Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
Execute following command if the result of your previous query is empty
SHOW columns FROM your-table;
For more details check this.
I'm not sure if it will satisfy you but you can do this
SELECT *, COUNT(*) FROM table;
It will return null values (except last column which you can ignore) if the query is empty and you will be able to access all columns. It's not proper way of doing it and selecting names from INFORMATION_SCHEMA would be much better solution.
Please note that result is aggregated and you need to use GROUP BY to get more results if there are any.
You should ,
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_SCHEMA='yourdb'
AND TABLE_NAME='yourtablename';

My SQL query not retrieving all the data from the database

I have a table in my database with the name contact. It has the following columns: name, mobile and twon. The problem is that I'm trying to get all the twon='Dubai', but when I execute my query it only retrieves 81000 rows but the total was 130000. The other remaining rows are not appearing in my query.
My Query:
SELECT * FROM `contact` WHERE `twon` = 'Dubai'
Can anyone tell me where I am going wrong or help me to access all the data from table?
= will fetch the record only if the column value is exactly 'dubai'.
Try with LIKE,TRIM and LOWER:
SELECT * FROM `contact` WHERE LOWER(TRIM(`twon`)) LIKE '%dubai%'
This query will fetch the records if twon column contains the word 'dubai'.
Different things could be wrong.
If you post examples of rows that should be returned, it would help us.
Case Sensitivity
If case sensitivity is a problem (e.g. 'dubai' or 'DUBAI' are not returned), you can use the LOWER function
SELECT * FROM `contact` WHERE LOWER(`twon`) = 'dubai'
Extra blanks
In some cases, extra blanks in the column content would fail, for instance ' Dubai' and ' Dubai '. You can use the TRIM function to get rid of trailing and leading blanks.
SELECT * FROM `contact` WHERE TRIM(`twon`) = 'Dubai'
Combination
Combining the two will work, too.
SELECT * FROM `contact` WHERE LOWER(TRIM(`twon`)) = 'dubai'

Determine table existence

I got this piece of code from Mysql 4th edition to check table existence
SELECT * FROM table WHERE FALSE;
I cant quite under stand the where clause.
If im not mistaken there is no boolean type in mysql, so basically it been cast to
SELECT * FROM table WHERE 0;
And should it be a column on a where clause?
SELECT * FROM table WHERE column = false;
Any explaination greatly appreciated.
No it doesn't have to have column as operand :)
When you open mysql select manual than expressions you would find there simple_expr what should mean conditions like WHERE 1 (about booleans) but I understand it can be confusing (and it's rare to use conditions without columns).
When you do SELECT * FROM table WHERE FALSE; it's basically this:
if there's table `table`
return 0 rows (minimal database overhead) and valid resource
else
return false
Just take a look on return values from mysql_query.
I am assuming that MySQL 4.x supports ANSI. If so, you could try the following:
SELECT count(*)
FROM information_schema.tables
WHERE table_schema = <schema-or-db-name>
AND table_name = <table-or-view-name>
Checking on a Table's Existence
$sql="SELECT * FROM $table";
$result=mysql_query($sql);
if (!$result)
{
// table does not exist
}
To answer your question:
The where clause is used to retrieve the value of a column in the given table. For example, if your table consists of two columns (user_id, user_name), your query might look something like the following:
$sql="SELECT * FROM $table where user_id = 1";
Lastly, you may read more about the where clause at this link

How can I get just the column names for a given mysql query?

I am creating a utility which lets users enter a SQL query for the purposes of importing data to my database.
The first step is to show a list of resulting fields so the user can route them to the destination fields.
When users import from MSSQL, I can use SET FMTONLY ON to fetch the list of output columns that the query would produce if ran (assuming the query is valid in the first place).
I haven't been able to find a way to do this for MySQL. EXPLAIN doesn't list the resulting fields.
Given the following query:
SELECT CONCATENATE(first_name, " ", last_name) AS name, age, foo
FROM customers
ORDER BY name ASC;
I ultimately need to get a list of output fields only, like this:
{ "name", "age", "foo" }
How can I do this in MySQL?
SET FMTONLY ON still requires you to get the column names and types manually, it just generates an empty result set.
For MySQL, add a WHERE FALSE somewhere
SELECT CONCATENATE(first_name, " ", last_name) AS name, age, foo
FROM customers
WHERE FALSE
ORDER BY name ASC;
You get this lovely execution plan
"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
"1";"SIMPLE";NULL;NULL;NULL;NULL;NULL;NULL;NULL;"Impossible WHERE"
Then parse the columns as you would set fmtonly on with MSSQL
For complex queries (nested, group by, limit-ed), wrap it in a subquery
select * from (
<your wonderful brilliant complex query>
) x where false
MSSQL would have complained if the inner query contains ORDER BY without TOP, MySQL is ok with it.
I think you need to look at the resultsetmetada. I carries the number of columns, column name, and a few more about the result set.
I think you're looking for DESC {table_name}

Which logical operator do I want to use in this mysql statement?

Which is a better way to select ans and quest from the table?
SELECT * FROM tablename WHERE option='ans' OR option='quest'";
OR
SELECT * FROM tablename WHERE option='ans' AND option='quest'";
Thanks so much!
Your second statement is not going to return any results. A record cannot have option=ans and option=quest at the same time.
It's not a question of a 'better' way - only the first one works. Even though you want option=ans and option=quest in your results set, the WHERE clause is executed once per row. So you're telling MySQL "give me a row where option=quest and option=ans" i.e. option is two values at once, which is impossible. You actually want to get rows where either is true, which is why you use OR.
I think this reads better:
SELECT * FROM tablename WHERE option IN('ans','quest');
If the rows that represent question have option set to 'quest' and rows with answer have option set to 'ans' then you should use option='ans' OR option='quest'";. Also a row cannot represent both question and answer so using AND will not select any rows.
This select will return all rows whos options is ans or quest
SELECT * FROM tablename WHERE option='ans' OR option='quest'";
This select on the other hand will not return any rows since a column has only one of those values
SELECT * FROM tablename WHERE option='ans' AND option='quest'";
Use this if you want your search to return both answers and questions:
SELECT * FROM tablename WHERE option='ans' OR option='quest';
It can also be written:
SELECT * FROM tablename WHERE option in ('ans','quest');