I have data table "menu" like this
id name type
-----------------------------
10 tea drink
20 krabby patties food
30 coffee drink
40 kelpo food
50 kelp shake drink
I want to select all id of drink, like this
id
---
10
30
50
i'm sorry, can you help me?
Select id from menu where type = 'drink';
This is a basic SQL you can simple search and find it
SELECT ID FROM menu WHERE TYPE='drink';
You can even use IN Operator
Query
SELECT id FROM menu
WHERE type in ('drink');
Related
I have this Menu for a restaurant with food categorized into different groups i.e.:
For the Breakfast (food here)
Salads (food here)
Cold Beverages (drinks here)
Hot Beverages (drinks here)
etc. ...
I've already created a table for the menu, I want to list the food served under Breakfast, Lunch, Dinner, Deserts, Drinks (hot and cold), Alcoholics, Smokes (shisha, pipes, cigarettes , etc..)
SQL has no function to serve my needs so... who has a creative idea!.
Of course, this can be done by adding the category title to the table and making my way around with indexing skipping the index of the row holding the title, but if the restaurant edits the menu ONCE everything will be scrambled.
That sounds like you're thinking of an SQL table like it was a Word table with different kinds of rows (like heading rows and data rows). That's not the case; an SQL table's rows should all be alike.
If your menu table is something like
id
name
price
1
Omelette
8
2
Sandwich
6
3
Soup of the Day
8
the simplest way to categorize these is to add a new column for the category:
id
name
price
category
1
Omelette
8
Breakfast
2
Sandwich
6
Breakfast
3
Soup of the Day
8
Lunch
(though in a real database design the category would probably be a foreign key to a Categories table)
You can then use a WHERE category = ... clause to only show some entries, or do SELECT DISTINCT category FROM menu ORDER BY category to get all of the categories in alphabetical order.
Lets say I have two tables, and in this example, the fish table has Place as a foreign key. In a select statement, how can I get the average weight per place, but instead of showing the id, it shows either the name of the place or the country that has the same id.
Fish
ID Type Weigth Place
1 Cod 300 1
2 Pike 600 2
3 Pike 1000 2
4 Salmon 800 1
Place
ID Name Country
1 NY USA
2 London UK
3 Oslo Norway
I can only figure out how to get average weight per place and return the id, but not the name or country of the place. I think I need to use join of some sort, but cant figure out how.
Thanks in advance
I have this table called items and I enabled full text searching on name column
id name price
---- ------ -------
1 brown wood 550
2 black wood 430
3 wooden chair 15
4 kitchen knife 3
5 sponge ball 1.35
I want to write a query to select all items whom name doesn't include 'wood' using full text search
so the result would be
id name price
---- ------ -------
4 kitchen knife 3
5 sponge ball 1.35
here is my query
SELECT * FROM items WHERE Match(name) Against('-wood' IN BOOLEAN MODE);
If you don't want wood, then the fist stab at a query would be:
SELECT *
FROM items
WHERE Match(name) Against('-wood*' IN BOOLEAN MODE);
However, the - only works after other terms are matched. So, you need some sort of positive match. Something like:
SELECT *
FROM items
WHERE Match(name) Against('k* and -wood*' IN BOOLEAN MODE);
If you know that such exclusions are common, you might include a particular word in every name (say, "name"). Alternatively, you might need to resort to like:
select *
from items i
where concat(' ', name' not like '% wood%';
I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.
I have a table with 3 columns: Animal, key, owner. Example
Cat 1 Bob
Bird 2 Bob
dog 3 Bob
dog 4 Andy
Lizard 5 Andy
Bird 6 Andy
Cat 7 Andy
and one related table per animal (columns key, weight). For example, table CAT_WEIGHT:
1 12
7 17
I want to find the Min, Max, Average, Total, and Count for each animal type, but only for a particular owner.
Is this possible to calculate these using a single MYSQL query? I know I can do it in multiple queries but am looking for the best way.
Thanks
Yes it is possible to do this using just one query.
All other things being equal you want to use as few queries as possible. Roundtrips to the database are generally some of the more expensive things you'll encounter in programs.
select
animal,
min(weight) min_weight,
max(weight) max_weight,
avg(weight) avg_weight,
sum(weight) tot_weight,
count(weight) cnt_weight
from
your_table
group by
animal
order by animal;
How about:
select min(Weight), max(Weight), sum(Weight), count(*) from animals_table group by animal