Forming SQL query - mysql

In my android app, I am getting a list of nearby restaurants from Google Place API.
But unfortunately, this list does not give menus of the restaurant.
I have T_RESTAURANT and T_MENU tables.
Lets say, I get 4 restaurants in the list returned by API, then how should I make my query to extract data.
If I do:
SELECT name, votes, review FROM T_MENU WHERE restaurant_name = REST_NAME_1;
and I have to fire this query for each of the restaurants i.e. 4 times in this case.
Can anyone suggest me a good solution?

SELECT name,
votes,
review
FROM T_MENU
WHERE restaurant_name
IN ( <four restaurant names comma separated> )

You can use "IN" keyword instead of "=".
A also suggest using restaurant ids instead of names.

try this:
select T.name, T.votes, T.review ,M.<menu_items>
from T_RESTAURANT R join T_MENU M
where R.restaurant_name=T.restaurant_name

Related

Count and distinct in mysql (comma separated data in table)

I am trying to get the details from mysql database.
My table have location field.
Data is like below
Location
India
UK,Finland, India
USA, NZ, AUS, Spain
I am trying to get the count for each location.
select count(distinct location) from posting
This is the query i have written at the moment for testing purpose. It return me count.
I want to get the details like below.
India -2
UK - 1
Finland -1
USA - 1
Something like that.
Note Please ignore dash as this is just to show you what i want.
Please advise me as i am not good in database.
You should not be storing multiple values in a single columns. Delimited strings are not the right way to store values in SQL. The proper way is a junction table.
If I assume that you have a list of locations, then you can do:
select l.location, count(*)
from location l join
posting p
on find_in_set(l.location, p.location) > 0;
If you don't have such a table, I would recommend that you focus on fixing your data structure first, rather than trying to get such a query to work.
SELECT COUNT(location),location FROM posting;
Try this one out. It will give you a similar output.

count number of repeating entries

I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id

Query using two tables with DISTINCT

I have two tables - clients and - group
I need to get county and zip from clients and group-assigned from group
When I search, I cannot get distinct results, that is, instead of the output showing 100 clients with zipcode 12345 in jones county in main st group.
I need to have each zip and county listed once by group. I have googled and attempted many ways but it is just beyond me.
Can anyone assist in steering me to the correct way
Adding GROUP BY group, city, zip to the end of your query should get you what you need. It will only return unique combinations of the three.
Presumably you have something like:
select g.*, c.county, c.zip
from clients c join groups g on <some join condition>
You want one result per group. So, add a group by clause such as:
group by g.id -- assuming id uniquely identifies each group
This will give an arbitrary value for the other fields, which may be sufficient for what you are doing. (This uses a MySQL features called Hidden Columns.)

Query MySQL for rows that share a value, and returning them as columns?

This is for a homework assignment. I haven't copy-pasted the question below, I made an simpler version of it that focuses on the specific area where I'm stuck.
Let's say I have a table of two values: a person's name, and the place he had lunch yesterday. Assume everyone has lunch in pairs. How can I query the database to return all the pairs of people that had lunch together yesterday? Each pair must be only listed once.
I'm actually not even sure what the professor means by return them as pairs. I've sent him an email, but no reply yet. It seems like he wants me to write a query that returns a table with column 1 as person 1 and column 2 as person 2.
Any suggestions on how to go about this? Does it seem right to assume he wants them as separate columns?
So far, I basically have:
SELECT name, restaurant FROM lunches GROUP BY restaurant, name
which essentially just reorganizes the table so that the people who had lunch together are one after the other.
We have to assume there can be only one pair eating lunch in a given restaurant.
You can get a list of pairs either using self-join:
SELECT l1.name, l2.name FROM lunches l1
JOIN lunches l2
ON l1.restaurant = l2.restaurant AND l1.name < l2.name
or using GROUP BY:
SELECT GROUP_CONCAT(name) FROM lunches
GROUP BY restaurant
The first query will return pairs in two different columns, while the second in one column, using comma as separator (default for GROUP_CONCAT, you can change it to whatever you wish).
Also note that for the first query names in pairs will come in alphabetical order as we use < instead of <> to avoid listing each pair twice.

Confused on SQL assignment

We are doing a database query in class. And it's using relational keys. I don't know how to get the query to run. Here is what is says.
For each movie, list its number and title, along with the number and name of the actors who appeared in it.
This is what I have, but it doesn't work
SELECT `Movie`,`Movie_ID`,`ActorNum` FROM `Movies`
Union
Select Actor.Fname, Actor.Lname FROM Actor
;
Im not sure what all the column names are but if ActroName would be the actors name would this be what you are looking for?
SELECT Movies.Movie,
Movies.Movie_ID,
Movies.ActroNum,
Actor.ActroName
FROM Movies
JOIN Actor ON
Actor.ActroNum = Movies.ActroNum