Selecting just one column instead of all columns in a mysql query - mysql

I am writing a PHP script where I will search for a keyword in a specific table and return the id of that keyword.
What I have done so far is:
SELECT * FROM 'table' WHERE column_name LIKE '%keyword%'
This returns the entire row of the keyword. What I want is a query that will just return the id of that row, so I can put it in a variable.

First, you should probably replace the quotes around 'table' with backticks, or you'll likely get syntax errors somewhere. The rest of my query examples use backticks for the table name. It's advised to use them for column names too to reduce the chance of conflicts.
Instead of using * to fetch all columns, explicitly list which columns you want to fetch:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'
The * wildcard selects all columns as you have noticed from your original query.
If you want to select other columns, list them as comma separated names:
SELECT id, foo, bar FROM `table` WHERE column_name LIKE '%keyword%'

Since you are using mySQL:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'

Related

SQL query - I don't know all identifiers

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String
To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1
You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;
You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

MySQL combining two where operators WHERE row LIKE '%$search%' AND WHERE NOT row='$id'

SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id';
I want to select all the results from my database that are like my search term, but exclude the row that is equal to my php variable $id.
Is it possible to do this in a MySQL query?
Put the all the conditions inside a single WHERE block and use conditional operators like AND/OR/NOT to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements

Oracle SQL - Regex to search columns with only 1 letter

I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')

MySQL get all characters before specific character

I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...

How to query comma delimited field of table to see if number is within the field

I want to select rows from a table if a column contains a certain number e.g 981. The data in these columns is either 0, null or in the format below:
1007,1035,1189,908,977,974,979,973,982,981,1007
How do I phrase this in a query?
Obvious this query below isn't sufficient but I need something similar
SELECT * FROM `table` WHERE `column`='981'
Thanks
If you have regular expressions it may work:
SELECT * FROM `table` WHERE `column` REGEXP '(^|,)951($|,)'
You can use the IN clause which checks whether a value is within a set of values.
SELECT * FROM `table` WHERE `column` IN (1007,1035,1189,979,973,982,981,1007)
or
SELECT * FROM `table` WHERE `column` IN ('abc','def','ghi')
Please note that cannot mix quoted and unquoted values in the IN list
EDIT
I misunderstood the original question. If your column data is like 1007,1035,1189,979,973,982,981,1007 and you're searching for the presence of 981 then you'll have to use LIKE instead of IN
SELECT * FROM table WHERE (column LIKE ('%,981,%') OR column LIKE ('%,981') OR column LIKE ('981,%'))
to pattern match the value 981 in the middle, at the end or at the beginning of those comma separated values.