If I have a table called Table - Food_Preferences as seen below
NAME FOOD COUNTRY
Jim Rice US
Jim Salad US
Jim Dessert US
Kim Rice JAP
Kim Salad JAP
Tim Rice UK
Tim Dessert UK
I want a query which gives result as below (Show Rice only if country is JAP else show other Foods)
Jim Salad US
Jim Dessert US
Kim Rice JAP
Kim Salad JAP
Tim Dessert UK
Try this query
select
*
from
myTable
where
1 = case
when FOOD = 'Rice' and COUNTRY <> 'JAP' then 0
else 1
end
Related
I have this Existing table tb1 in my database
Now new data comes and new data is stored in another table tb2
Earlier Account_Number 9988 was Level 2, But now in the new table it is Level 1
So the updated table tb1 should be :
How to achieve this result?
Below example can be used to resolve your issue.
INSERT INTO table_name TABLE table_name;
Above query should work
-- Assuming the visiting_students table has already been created and populated.
> SELECT * FROM visiting_students;
name address student_id
------------- --------------------- ----------
Fleur Laurent 345 Copper St, London 777777
Gordon Martin 779 Lake Ave, Oxford 888888
> INSERT INTO students TABLE visiting_students;
> SELECT * FROM students;
name address student_id
------------- ------------------------- ----------
Amy Smith 123 Park Ave,San Jose 111111
Bob Brown 456 Taylor St, Cupertino 222222
Cathy Johnson 789 Race Ave, Palo Alto 333333
Dora Williams 134 Forest Ave, Melo Park 444444
Fleur Laurent 345 Copper St, London 777777
Gordon Martin 779 Lake Ave, Oxford 888888
Refer - https://docs.databricks.com/sql/language-manual/sql-ref-syntax-dml-insert-into.html#insert-using-a-table-clause
I have a table that stores id, item, food type, image and price
I want to limit the result to one of each type sold in the store e.g. dog food, cat food, dvd in descending order
Example of what is held in mysql database
table: Nunca
id item type image price
=========================================================
1 Pedigree Pouches dog food img1 $4.99
2 Cesar Classic Selection dog food img2 $3.99
3 Meaty Strips Chicken dog food img3 $2.99
4 Jelly Fish Selection cat food img4 $1.99
5 Bananas fruit img5 $2.84
6 Celery vegetables img6 $5.99
7 Apple fruit img7 $0.95
8 Pineapple Juice Soft drink img8 $0.99
9 Oranges fruit img9 $1.99
10 Cabbage vegetables img10 $0.99
11 Suicide Squad dvd img11 $12.99
The final result should look like this from the query
id item type image price
==========================================================
11 Suicide Squad dvd img11 $12.99
10 Cabbage vegetables img10 $0.99
9 Oranges fruit img9 $1.99
8 Pineapple Juice Soft drink img8 $0.99
4 Jelly Fish Selection cat food img4 $1.99
3 Meaty Strips Chicken dog food img3 $2.99
I have tried various mysql queries, but to no avail
select * from t where
id in(select max(t.id) id from t group by t.type)
order by id desc
I guess there can be window or join alternatives but this one seemed fitting for this purpose.
A typical method uses a correlated subquery:
select n.*
from nunca n
where n.id = (select max(n2.id) from nunca n2 where n2.type = n.type);
This will choose the last row of each type.
I have many users that can select their choice from rank 1-4.
The table look like this:
username rank1 rank2 rank3 rank4
Perter Apple Orange Grape Melon
Adam Orange Melon Apple Grape
James Melon Grape Orange Apple
How do I get a table like this in php mysql
username Apple Orange Grape Melon
Peter 1st-choice 2nd-choice 3rd-choice 4th-choice
Adam 3rd-choice 1st-choice 4th-choice 2nd-choice
Jame 4th-choice 3rd-choice 2nd-choice 1st-choice
My table has three columns 'name', 'city' and 'country'.
Now I want to make a list with only those countries, which has at least 3 times the SAME city.
name city country
---- ---- -------
Smith Boston USA
Wayne St. Louis USA
Miller Houston USA
Joseph Houston USA
Obama Washington USA
Jones Houston USA
Sarkozy Paris France
Merle Paris France
Gabin Marseille France
Delon Avignon France
Deneuve Avignon France
Trappatoni Rome Italy
Linguini Milano Italy
Mastroianni Rome Italy
Meier Hamburg Germany
Müller Munich Germany
Schmidt Hamburg Germany
Böttcher Hamburg Germany
Kunau Hannover Germany
Wilhelm Munich Germany
-------------------------------
USA
Result:
Germany
I tried it with distinct, count, group by etc. But without results.
Group by country and city then take only those having at least 3 entries each.
select distinct country
from your_table
group by country, city
having count(*) >= 3
I've have gone through stackoverflow for days and googled a lot but can still find the solution.
I have 2 tables profile and qualification
in the table profile
pro_id surname firstname
-------- -------- ------------
1 John James
2 King Fred
3 Paul Smith
4 Cindy Hayes
Qua_id Degree School Year
------ ------ ------ -----
1 MBA Wharton university 2002
1 LLB Yale University 2001
2 BSc Covington University 1998
2 BEd Kellog University 1995
2 Msc MIT 2011
3 MBA Havard Business School 2002
3 MSc Yale University 2012
4 BSc University of Edinburgh 2010
4 BA University of Liverpool 2009
Now what i want to achieve is this
1 John James MBA Wharton university 2002, LLB Yale University 2001
2 King Fred BSc Covington University 1998, BEd Kellog University 1995, Msc MIT 2011
3 Paul Smith MBA Havard Business School 2002, MSc Yale University 2012
4 Cindy Hayes BSc University of Edinburgh 2010, BA University of Liverpool 2009
But what i currently have is :
1 John James MBA Wharton university 2002
1 John James LLB Yale University 2001
2 King Fred BSc Covington University 1998 BEd
2 King Fred Kellog University 1995
2 King Fred Msc MIT 2011
3 Paul Smith MBA Havard Business School 2002
3 Paul Smith MSc Yale University 2012
4 Cindy Hayes BSc University of Edinburgh 2010
4 Cindy Hayes BA University of Liverpool 2009
Here is my code
Select pro_id, surname, firstname concat(degree,school,year) as qual from profile,qualification Where profile.proid=qualification.qua_id
Select pro_id, surname, firstname, group_concat(degree,school,year) as qual
from profile
left join qualification
on qualification.qua_id = profile.pro_id
group by qua_id
your filed names look a little odd - qua_id should perhaps be profile_id (which I think shoudl really be user_id - ie they should both be a user id and foreign keys to a user table).