mysql select all table names except - sql

Alright, I know you can do the following to get all column names.
SHOW COLUMNS FROM person;
However, it does not allow where statements.
Is there any way to get the full list and just say not this one?
So I am basically looking for something like this:
select (show columns from person where Field <> 'id') from person

USe SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Example:
SHOW CHARACTER SET WHERE `Default collation` LIKE '%japanese%';
More about show here
Try something like SHOW COLUMNS FROM person WHERE column != 'value';

Or you can use proper a SQL query against information_schema
select * from information_schema.columns
where table_schema = 'test' # your db name
and table_name = 'person '
and column_name != 'id'
For MySQL 4, you can use LIKE directly instead of WHERE http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
SHOW COLUMNS FROM 'Person' LIKE '%x%'
However unfortunately, that does not support a NOT clause.
If I may suggest
If you need this information from
MySQL console/phpAdmin/equivalent,
just eyeball it..
If you need this
from a front-end program like php, do
it from that environment.

MySQL documentation says that WHERE clause is allowed. Read the docs.

You cannot perform a true SHOW COLUMNS WHERE in MySQL 4. The docs show only an optional LIKE parameter for that version: http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
However, you can use the metadatabase information_schema.
USE information_schema;
SELECT * FROM `COLUMNS` WHERE `TABLE_NAME` = 'person' AND `COLUMN_NAME` != 'value';

Related

SQL Match table and multiple columns

I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.

Compare strings in SQL

I am in a situation where I need to return results if some conditions on the string/character are met.
For example: to return only the names that contain 'F' character from the Person table.
How to create an SQL query based on such conditions? Is there any link to a documentation that explains how can SQL perform such queries?
Thanks in advance
The most basic approach is to use LIKE operator:
-- name starts with 'F'
SELECT * FROM person WHERE name LIKE 'F%'
-- name contains 'F'
SELECT * FROM person WHERE name LIKE '%F%'
(% is a wildcard)
Most RDBMS offer string operations which are able to perform that required task in one way or the other.
In MySQL you might use INSTR:
SELECT *
FROM yourtable
WHERE INSTR(Person, 'F') > 0;
In Oracle, this can be done, too.
In PostgreSQL, you can use STRPOS:
SELECT *
FROM yourtable
WHERE strpos(Person, 'F') > 0;
Usually there are several approaches to solve this, many would choose the LIKE operator. For more details, please refer to the documentation of the RDBMS of your choice.
Update
As requested by the questioner a few words about the LIKE operator, which are used not only in MySQL or Oracle, but in other RDBMS, too.
The use of LIKE will in some cases make your RDBMS try to use an index, it usually does not not try to do so if you use a string functions.
Example:
SELECT *
FROM yourtable
WHERE Person LIKE 'F%';
The query may look like this:
SELECT * FROM Person WHERE FirstName LIKE '%F%' OR LastName LIKE '%F%'

How can I Retrieve a List of All Table Names from a SQL Server CE database?

Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';

can anyone please help me understand this query?

Can anyone help me understand this query? The mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm part mostly. Generally it is table_name.columnName but here the format seems to be different
SELECT mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm,
mktpgm.mktgpgm,
mktpgmvhclxref.cmpgn,
campaign.cmpgndescription,
mktpgmvhclxref.mktpgmvhclxrefid.vhcl,
vhclhierarchy.modname,
mktpgmvhclxref.mktpgmvhclxrefid.modyr,
mktpgmvhclxref.userinsrt,
mktpgmvhclxref.rowinsrt
FROM mktpgmvhclxref mktpgmvhclxref,
mktpgm mktpgm,
vhclhierarchy vhclHierarchy,
campaign campaign
WHERE mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm = mktpgm.idmktgpgm
AND mktpgmvhclxref.cmpgn = campaign.campaignid
AND mktpgmvhclxref.mktpgmvhclxrefid.vhcl = vhclhierarchy.vhcl
Often, the first identifier is the schema / database name, depending on your database:
[schema].[table].[column]
In most databases, [schema] and [table] qualifiers are optional, if the [column] name is unambiguous.
In your case, however, I doubt that this is the actual case as mktpgmvhclxref is a table in your FROM clause. Oracle for instance, also knows user-defined types (UDTs, OBJECT types). So I'm guessing that:
mktpgmvhclxref.mktpgmvhclxrefid.mktgpgm
Corresponds to
mktpgmvhclxref = table
mktpgmvhclxrefid = column
mktgpgm = UDT attribute
If you're using Oracle, you can probably find your UDT as such:
select *
from all_type_attrs
where (owner, type_name) = ((
select data_type_owner, data_type
from all_tab_cols
where table_name = 'MKTPGMVHCLXREF'
and column_name = 'MKTPGMVHCLXREFID'
))
order by attr_no
mktpgmvhclxref.mktpgmvhclxrefId.mktgPgm
[--Schema----].[----table-----].[Column]
i got the anwer to my question .
its related to hibernate mapping.
The second field is actually for composite key of a table .

How do I display records containing specific information in SQl

How do I select all records that contain "LCS" within the title column in sql.
SELECT * FROM TABLE WHERE TABLE.TITLE LIKE '%LCS%';
% is the wild card matcher.
Look into the LIKE clause
Are you looking for all the tables with a column name which contains the LCS in them? If yes the do this
select table_name
from information_schema.columns
where column_name like '%lcs%'