Error in Mysql query with LIKE %...% - mysql

I am trying to update a table using another table. I am not able to determine how to use LIKE %…% feature here. Please help me out.
Table1:
TableToBeUpdated:
id | location | value
------------
1 | california | I am going to be here soon.
2 | Nill | Hello I love playing chess and yes.
3 | Nill | my hotel room is just shitty!
4 | Nill | Why on earth God doesn’t live on earth!
5 | Nill | friends of friends and their dogs.
Table2:
TableToCheckFrom :
uniqueid | location | keyword
---------------------
1 | Texas | Why on earth
2 | NewYork | friends and their
3 | Washington | love playing chess
4 | NewYork | their dogs
The result should be:
id | location | value
------------
1 | California | I am going to be here soon.
2 | Washington | Hello I love playing chess and yes.
3 | Nill | my hotel room is just shitty!
4 | Texas | Why on earth God doesn’t live on earth!
5 |NewYork| friends of friends and their dogs.
——
I am using this formula but its giving me constant error:
UPDATE TableToBeUpdated, TableToCheckFrom
SET TableToBeUpdated.location = TableToCheckFrom.Location
WHERE TableToBeUpdated.Value LIKE %TableToCheckFrom.Keyword%
Thanks in Advance!

You need to put the % in quotes and concatenate them to the keyword. And you need to join the two tables so you can refer to columns from both of them.
UPDATE TableToBeUpdated AS u
JOIN TableToCheckFrom AS c ON u.Value LIKE CONCAT('%', c.Keyword, '%')
SET u.location = c.Location

You query is wrong (you never tell the DBM that you want to access the TableToCheckFrom table).
For an idea on how write such queries look at this question
Update mysql table with data from another table
If you find the like %% as an additional difficult strip it: first prepare a working query with a simply condition and then add the like one.

Related

What is a right way to use like in SQL

I am working on "Not Boring Movies" problem in leetcode.
The porblem describes as following
"X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.
Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating."
For example, table cinema
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card| Interesting| 9.1 |
+---------+-----------+--------------+-----------+
For the example above, the output should be:
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
+---------+-----------+--------------+-----------+
My solution is:
SELECT id, movie ,description, rating
FROM cinema
WHERE ID % 2 != 0 AND description NOT LIKE %boring%
ORDER BY rating DESC
This solution can not past leetcode test.
The expected solution is:
SELECT id, movie ,description, rating
FROM cinema
WHERE id % 2 != 0 AND description != 'boring'
ORDER BY rating DESC
Basically, both solution follow the same idea to solve the problem, but I can not understand the difference between them. Can anyone tell me why my solution falls to past the test?
The answer isn't accepted, because they want you to filter out rows with descriptions that are exactly "boring", and not rows that have descriptions like, e.g., "not boring" or "only a little boring". Your query would filter out all three rows.
The problem description clearly states ...description that is not 'boring'. It doesn’t say “not containing”, it says “is not”
contain is LIKE in sql and is is = in sql
LIKE %boring% will select
_boring!
...boring...
etc
But the description value is just "boring"
you should use ('%boring% ') on your like
SELECT id, movie ,description, rating
FROM cinema
WHERE ID % 2 != 0 AND description NOT LIKE '%boring%'
ORDER BY rating DESC

MySQL query for two users with common responses to a survey

I have a MySQL table with users who have completed a survey - in some cases, they have complete the survey multiple times. So it looks like this:
users|survey_attempt|question_num|response
---------------------------------------------
john | 1 | 1 | cat
john | 1 | 2 | dog
john | 1 | 3 | frog
john | 2 | 1 | dog
john | 2 | 2 | frog
john | 2 | 3 | dog
jim | 1 | 1 | frog
jim | 1 | 2 | bat
jim | 1 | 3 | bat
jim | 2 | 1 | cat
jim | 2 | 2 | frog
jim | 2 | 3 | bat
In this case, how would I find users who had common responses within the same attempt at the survey? So for instance, if I wanted to know who answered "frog" and "cat" within a unique attempt at the survey (regardless of which specific question the answer was for)?
In general, the database layout has flaws. I would suggest to use unique survey submission IDs. Because right now, you need to check user name AND survey attempt to determine if two or more rows belong to the same submission.
Anyways, you would need to self join the table and check for the answer you want but disregard the question:
SELECT A.users, A.survey_attempt
FROM table A
INNER JOIN table B ON A.users = B.users AND A.survey_attempt = B.survey_attempt
WHERE A.response = 'frog'
AND B.response = 'cat';
The table is matched with itself, in each result table you'll have all columns two times. Then the query will only select these rows where both user names and survey attempt numbers are equal. Finally, the WHERE statement checks for the answers you wanted. Nowhere, the question number is checked as you wanted to get the result regardless of specific questions.

MySQL - join on same table?

I'm trying to implement this answer, which shows how, using table aliases, you can effectively join to the same table you're running the query on.
Consider the following (simplified) table of football teams:
-----------------------------------------------------
| id | name | next_fixture_against | url_token |
=====================================================
| 1 | Hull City | 2 | hull |
-----------------------------------------------------
| 2 | Everton | 1 | everton |
-----------------------------------------------------
I'm trying to write a query that fetches the names of a given team and its next opponent.
Approach 1: left join
Here's what I came up with by adapting the above answer:
SELECT main_team.name, opposition_team.name as against
FROM teams AS main_team
LEFT JOIN teams AS opposition_team
ON main_team.name = opposition_team.next_fixture_against
WHERE main_team.url_token = 'hull'
That produces:
-----------------------
| name | against |
=======================
| Hull City | NULL |
-----------------------
If I remove the WHERE clause, it does work, but, strangely, it comes out in the reverse order from that which I'd expect, i.e.
-----------------------
| name | against |
=======================
| Everton | Hull City |
-----------------------
rather than name = Hull City, against = Everton.
(This is purely a point of interest - clearly I need the where clause as I'm targeting a particular team, not all teams.)
Approach 2: sub-query
I came up with the following, alternate approach:
SELECT
name,
(SELECT name FROM teams WHERE id = ???) AS against
FROM
teams
WHERE
url_token = 'hull'
...but I'm not sure what to replace ??? with to get the inner sub-query to reference the outer one.
Can anyone point me in the right direction? Thanks in advance.
You need ON main_team.id = opposition_team.next_fixture_against. You tried to use the team name rather than id in the join.
Don't worry, we've all done this.

Optimising table design or optimising the query

I am trying to decide which one is better: to design a table that wastes a lot of space and has a simple query OR to write a very tight table but then the process of finding what I am looking for would be very processing intense.
The actual problem is this:
Imagine you have a very simple table. 1st column for the ID number the 2nd is a list of names and the 3rd is a list of names too. The 2nd column is a list of people who owe to the people in the 3rd column.
The search should do the following:
I search for a name in the 3rd column and see who owes this person in the 2nd column. A name or multiple names come up, then I want to see who owes them, again a bunch of names come up, and so on to level 5.
Maybe this is a well known scheme for which there is a well known simple answer in table design or MySQL circles. Could anybody suggest a MySQL query or perhaps an appropriate table design where I can use a simple query?
Example
ID owes owned to
1 Peter John
2 John George
3 Abdul George
4 George Anna
So I could design a wasteful table like this
ID 1 2 3 4 5
1 Anna George Abdul
2 Anna George John Peter
3 George Abdul
4 George John Peter
5 John Peter
But this would be very wasteful and bad bad design but it would be very easy to access the data along with the hierarchy and the owing chain.
Something like this seems suitable:
people
+----+--------+
| id | name |
+----+--------+
| 1 | Marty |
| 2 | Steven |
| 3 | John |
+----+--------+
With the table building the relationships between people owed and owing:
loans
+-----------+-------------+
| lender_id | borrower_id |
+-----------+-------------+
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
+-----------+-------------+
You could get all the people owing a given lender with something as simple as:
SELECT people.id, people.name
FROM loans
INNER JOIN people ON people.id = loans.borrower_id
WHERE loans.lender_id = X
Where X is the id of the lender. Given the lender_id of 1 (Marty) for example would yield:
+----+--------+
| id | name |
+----+--------+
| 2 | Steven |
| 3 | John |
+----+--------+
You can repeat this process for each of the resulting people until there are no results (no one being owed).

Completing a MAX() but using data from a few more tables

well I eventually got to a max() on one table pulling correctly using max() - took me sometime to understand what was going on and reading the limits of mysql when using it
I have spent a some time doing some demo data on sqlfiddle (one below is with just the max on the one table
http://sqlfiddle.com/#!2/fff224/1
what i would like (and to absorb as ive tried for the last 2 hours on getting this to work) is how to incorporate another tables that dont need to use max (i have included these in the sqlfiddle
the result i would be after would be
case_number full_address case owner client compiled date(max()) recommendation
1000 1 high street bob london 14/12/2012 let
1001 2 high street ken Compton 13/12/2013 sell
1002 3 high street ken Leeds 14/12/2013 sell
completing the inner joins between from client\staff\ to case process im fine with its just this nested select max im falling over on
SELECT p.case_number
, p.full_address
, s.case_owner_name 'case owner'
, c.client_name client
, r.compiled_date
, r.recommendation
FROM case_process p
JOIN staff s
ON s.case_owner_number = p.case_owner_number
JOIN client c
ON c.client_number = p.client_number
JOIN reporting r
ON r.case_number = p.case_number
JOIN (SELECT case_number,MAX(compiled_date) max_compiled_date FROM reporting GROUP BY case_number) x
ON x.case_number = r.case_number
AND x.max_compiled_date = r.compiled_date;
+-------------+---------------+------------+---------+---------------+----------------+
| case_number | full_address | case owner | client | compiled_date | recommendation |
+-------------+---------------+------------+---------+---------------+----------------+
| 1000 | 1 high street | Bob | London | 2012-12-14 | let |
| 1001 | 2 high street | Ken | Compton | 2013-12-13 | sell |
| 1002 | 3 high street | Ken | Leeds | 2012-12-14 | sell |
+-------------+---------------+------------+---------+---------------+----------------+
3 rows in set (0.00 sec)
fiddle of same... http://sqlfiddle.com/#!2/fff224/7