How do I Show columns for specific fields .
SHOW COLUMNS FROM core_banking_mpesa WHERE FIELD= 'id' , FIELD ='LineNo' , FIELD ='Comments'
Your error is that WHERE column1='val', column2='val' is not valid syntax.
You could use IN() to select the fields:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD IN('id','LineNo','Comments')
Or use OR:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD='id' OR FIELD='LineNo' OR FIELD='Comments'
You could try just doing a SELECT query instead, requesting those three columns:
SELECT id, LineNo, Comments
FROM core_banking_mpesa;
If you already know what the columns are, it doesn't make much sense why you would want to use SHOW COLUMNS on this table.
First of all, in the database world, COLUMN and FIELD mean the same thing. So you can't "show columns for specific fields".
However if I understand correctly, you are trying to display data from a MySQL table for a specific set of columns/fields. If that is the case, you can try this. Use SELECT query this way :
select column_name
from information_schema.columns c
where table_name = 'core_banking_mpesa';
Related
I'm trying to make my database searchable. Some of my tables are products, categories, subCategories manufacturedYear, model, brands. I'm using "%LIKE%" on my query and then union on tables. I get a result back, but one of the problems I cannot get the table name. One solution I thought of is creating a searchable table with 4 columns:
id, searchable term (it could be a sku,category ,subcategory etc ) and ID of the searchable term.
You can do this:
...UNION ALL SELECT 'mytable' AS tablename, term FROM mytable ... Basically, you can put a simple string value in with a virtual field in each sub select of the union. In this case I just called it tablename with a value of "mytable" the name of this example table.
So each row in mytable will have a field added named tablename with a string value of mytable. Then as you union the results, each result set (Sub-Select) could have a different string value (in tablename) in the query for their respective tables.
You can also refer to this post which is specifically on "virtual columns"
How to create virtual column using MySQL SELECT?
I don't know if I would say this is the "Right" way, but it will surly work.
Enjoy
Since you are using UNION already, why not just add the name of the table being queried as a column in each UNIONed subquery, like :
SELECT 'table1' AS table_name, id, field1, field2
FROM table1
WHERE value LIKE ?
UNION
SELECT 'table2' AS table_name, id, field11, field12
FROM table2
WHERE value LIKE ?
UNION
...
When you get the data, you can get the name of the table from where each record was returned by checking column table_name.
NB : it would also be a good idea to return the primary key of each row, to make possible further requests easier.
I have a table contain 22 columns
I have a query need to SELECT 20 columns
Is any way to do a query like NOT SELECT (the columns i don't want to select)
So I don't need to type SELECT column1, columns2...
You cannot do it as you expected. You have to type all the columns you want. If you have run the query many times, you can create a VIEW with selected columns.
No it is not possible, the expression "select all except" or "NOT SELECT" has not yet been implemented in any existing database.
The only way is to specify the columns you want or use the '*' wildcard
SELECT * FROM TABLE
or
SELECT column1, column2...
SQL doesn't allow to hide some columns. You can either select all columns by using SELECT * ... or list columns you need by SELECT col1, col2, ...
Please check this answer. It is the only way to do that
Select all columns except one in MySQL?
(I can't comment so I put the link as an answer)
Cheers
I have a table called page and I query this, for instance,
SELECT *
FROM page
and I access the data each row in this table columns like this $item->title;
I want to know if I can select the row of this table and at the same time I can retrieve what name is this table I am selecting? So when I do this,
echo $item->table_name; then I get this - page
is it possible?
You could, of course, do something like this:
SELECT *, 'page' AS table_name FROM page;
Asking what table you obtained a row from doesn't really make a whole lot of sense though. What would you expect the result to be if you had executed a join? What about a union of two queries that select different tables?
Actually, there appears to be a PHP function to give you the table name of your query. I've never used it, so am not sure how it would work in a join or anything, but it appears to do what you wish at the moment.
I have a db with 8 tables which intend to search for an id number by joining them but i would like to retrieve the name of the table to use in a php along the lines of...
if($tablename == 'accounts'){
echo 'value found in accounts';
}
What would the mysql query be?
(I don't need help with the joining etc, just the bit that would get the table name and how to store it in $tablename)
Thanks
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
19.12. The INFORMATION_SCHEMA TABLES Table
How can you search for data in a table if you don't know the table name in the first place?
So I suppose that you already know the table names, and don't need things like SHOW TABLES; that you have e.g. a WHERE of the form
WHERE (table1.field LIKE '%search%' OR table2.field2 LIKE '%search%' OR ...)
and once retrieved a row, you know it matches, but you don't know where the match did occur. And running eight straight queries instead of one JOINed isn't an option.
If this is the case, you'll have to supplement your SELECTed fields with a flag to tell you what you need:
SELECT ..., CASE
WHEN accounts.field1 LIKE '%search%' THEN 'accounts'
WHEN customers.name LIKE '%search%' THEN 'customers'
...
END AS wtf FROM <rest of your query>
Then in PHP when retrieving the row, you will just use the field:
print "Value found in {$row['wtf']}";
'SHOW TABLES' afair, is it still used on mysql? :)
I had my query set up the other day as so
$query = "SELECT card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest order by card_id";
As you can see, I was selecting card_id,title,description,meta_description,seo_keywords from the table cards, and price was coming from cheapest_card. They both have the card_id in common (in both tables). However, I ran into a bit of an issue. When I run the query in navicat lite, I receive an error "card_id is ambiguous". Was I doing something wrong?
When 2 or more tables have a column that is named the same, you have to qualify the table you want the column to be from.
i.e.:
$query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest order by card_id";
Furthermore, do you really want to run the query this way, without a WHERE/JOIN-clause to define how to JOIN the two tables?
$query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price
FROM cards,card_cheapest WHERE cards.card_id = card_cheapest.card_id
ORDER BY card_id";
When you have the same column name in two tables you're selecting from, you have to prefix the part in the SELECT with one of the table names (it doesn't matter which if it's the same data)
such as SELECT cards.card_id, ...
EDIT: However, cularis's answer is much more explanatory than mine, and take note about joining the two card_id columns if you want to get correct results.
When you run queries that get information from multiple tables with shared field names you need to specify from which table you want to extract what field. You do this by specifying the table name before the field name.
In your case you have two options:
cards.card_id or card_cheapest.card_id.
Also I agree with #cularis, you are probably better of doing a join, but still you will need to specify which card_id you want to select: the one from cards or card_cheapest.