Selecting all columns that start with XXX using a wildcard?
There's no way to do exactly what you're trying to. You could do another query first to fetch all the column names, then process them in PHP and build the second query, but that's probably more complex than just writing out the names that you want.
Or is there a reason this query needs to be dynamic? Will the table's structure change often?
SQL Column Name wildcard
You will want to build a dynamic query as explained here: https://stackoverflow.com/a/4797728/9553919
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Foods'
AND table_schema = 'YourDB'
AND column_name LIKE 'Vegetable%'
select column names that start with x
Try this
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME`='producten' AND `COLUMN_NAME` LIKE 'pweb%'
AND DATA_TYPE = 'int'
Dynamic SQL Column Selection
You can use dynamic unpivot
with the columns which you already selected. Following sample query might help you.
DECLARE
@sql NVARCHAR(MAX) = N'',
@cols NVARCHAR(MAX) = N'';
SELECT @cols += ', ' + QUOTENAME(name)
FROM sys.columns
WHERE [object_id] = OBJECT_ID('yourtable')
AND name LIKE '2020%';
SET @cols = STUFF(@cols, 1, 1, '')
SELECT @sql = N'SELECT Other_columns, date_columns, val
FROM
(
SELECT Other_columns, ' + @cols + '
FROM yourtable
) t
UNPIVOT
(
val FOR date_columns IN (' + @cols + ')
) up;';
PRINT @sql;
-- EXEC sp_executesql @sql;
Please find the final db<>fiddle here.
Find column whose name contains a specific string
Just iterate over DataFrame.columns
, now this is an example in which you will end up with a list of column names that match:
import pandas as pd
data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)
spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)
Output:
['hey spke', 'no', 'spike-2', 'spiked-in']
['spike-2', 'spiked-in']
Explanation:
df.columns
returns a list of column names[col for col in df.columns if 'spike' in col]
iterates over the listdf.columns
with the variablecol
and adds it to the resulting list ifcol
contains'spike'
. This syntax is list comprehension.
If you only want the resulting data set with the columns that match you can do this:
df2 = df.filter(regex='spike')
print(df2)
Output:
spike-2 spiked-in
0 1 7
1 2 8
2 3 9
Select all columns except one in MySQL?
Actually there is a way, you need to have permissions of course for doing this ...
SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_omit>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_omit>
Related Topics
How to Echo Out Table Rows from the Db (Php)
How to Check the Performance of MySQL Indexing
Why Is Mime_Content_Type() Deprecated in PHP
Php: How to Sort the Characters in a String
MySQL Select Query Within a Serialized Array
Retrieve JSON Post Data in Codeigniter
Laravel 5.4 Redirection to Custom Url After Login
How to Connect to MySQL Database in PHP Using MySQLi Extension
Calculate Skip Value for Given Record for Sorted Paging
Get Size of Post-Request in PHP
Understanding Pdo MySQL Transactions
Can't Concatenate 2 Arrays in PHP
How to Convert Boolean to String
Laravel Eloquent: How to Order Results of Related Models
Detecting Image Type from Base64 String in PHP
How to Identify Server Ip Address in PHP