SQL question.
I have 2 tables:
1. Clubs (2 columns - CID, Club)
2. Players (4 columns - Name,Position,CID,Club)
Situation:
In table 1 both columns are filled and have data.
In table 2 the columns Name, Position and club have data. The column CID is empty.
My aim is to fill the column CID (table 2) using the data from table 1 (CID) but only if the column Club (table 2) matches the value of the column Club (table 1).
I am pretty new to SQL, so not sure where to begin. I have been reading about UPDATE, JOIN but I'm a bit wary of how this should be accomplished.
Thanks in advance.
It is pretty simple query:
UPDATE Players
JOIN Clubs ON Clubs.Club = Players.Club
SET Players.CID = Clubs.CID;
Related
I am trying to write a SQL Query. I have 2 tables. Table 1(left table) and Table 2(right table). I want to do a left join. So If a Group in table 1 is found in table 2, we replace it with New Group.
Table 2 has all PRIME Group. There are 2 conditions:
If a PRIME (or) SEMIPRIME is there in table 1, we lookup in table 2 and replace group with new group if found.
If a PRIME is there in table 1,and does not exist in NewGroup(Table2) we omit that group itself.(highlighted in yellow).
I tried using coalesce(y.Newgroup,x.Group), but how do I include 2 conditions?
Please refer input tables and expected output here
I created table here: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f019fb942f3aae3d62427a0ac142d639
i'm working on an advanced search functionality on my website.
Basically data I'm working on is stored within two tables.
First table contains basic information about the product (1 row = 1 product so it's unique).
Table structure may look like this:
id, title, description
The second table contains more information about the product. The product may but don't have to have any rows here. However, one product may store in the second table a few rows. What's more - data in the second table should be used to the advanced search functionality. Table structure may looks like this:
id, item_id (from table 1), value_id (from another table), value
I want to select only these products (table 1) which has specified value_id (from column 2):
... WHERE table1.item_id = 5 AND table2.value_id = 1000
As I mentioned before - table 2 may but doesn't have to contains any rows connected by item_id with the table 1.
I've tried to use JOIN/LEFT JOIN function in SQL but in this case when the product has 3 rows in the table 2 - a query result returns 3 rows instead of 1 or 0 (if not found any results).
How can I handle that?
You want to select products. So select from the product table. You want to select only those for which exists a certain attribute. So create an approriate WHERE clause. As you want to look up data in another table, you could use EXISTS or IN.
select *
from items
where id in (select item_id from item_values where value_id = 1000);
i have a table 'A' with status column, it can have 4 values. In table A i have table 'B's id, table B have table 'C's id. I want to get the status count FROM table 'A' by joining all these columns. The status column in table A is a foreign key from table 'D'. Table 'D' having status like 1-agreed, 2-not agreed etc
The question is missing some information that might be helpful. Particularly, what exactly you are wanting to count. (i.e. are you just trying to count ALL rows, or are you trying to count the number of rows in table A that have each status). I'll put together an answer that assumes that latter.
I'll also just assume that "id" is the primary key of its own table, and that id will be the id from other tables inside a table.
select A.statusField, count(*)
from A
join B on (A.Bid = B.id)
join C on (B.Cid = C.id)
group by A.statusField
Hope that helps.
I have two tables, and I want to update data in one table based on conditions in both tables being satisfied.
Table 1 is called employee and contains the fields staffId,forename, surname and grade. Table 2 is called competence and contains the fields compId, staffId, groupId, subGroupId, skillLevel and skillUpdate. The common field is staffId.
The field skillLevel can be a value from 0 to 5 and I want to reduce this to 3 when anyone at 'Graduate' grade has set this to 4 or 5. How can I do that?
If I've understood your question properly then try something like this and let me know. :P
UPDATE competence AS a
INNER JOIN employee AS b ON a.staffId = b.staffId
SET a.skillLevel = 3
WHERE b.grade LIKE 'Graduate' AND a.skillLevel IN (4,5)
I have MySql database connected to my Java app. My users can choose meals, category of meals, quantity, write note and order that meals. I store that orders in table usluga_hrana. At the moment I am working only with meals service but I have other services too, like drink service, wake up service and others. Thats why I need one more table for all orders from different services and its called narudzba. Now I need to display values of all these atributes in one query: broj_sobe (room number, table narudzba), id_narudzba (id order, table narudzba), naziv_kategorija (category name, table kategorija_jela), naziv_hrane (name of meal, table naziv_jela), kolicina (quantity, table usluga_hrana), napomena (note, table usluga_hrana), datum_vrijeme (date and time, table usluga_hrana) and izvrseno (done, table narudzba). Problem is that all these atributes are in different tables and when I execute my query, it displays me multiple values of orders, meals etc.
My tables are connected this way:
PK id_usluga (table usluga) is FK id_usluga in table narudzba
PK id_usluga (table usluga) is FK id_usluga in table usluga_hrana
PK id_kategorija (table kategorija_jela) is FK id_kategorija in table usluga_hrana
PK id_hrana (table naziv_jela) is FK id_hrana in table usluga_hrana
PK id_kategorija (table kategorija_jela) is FK id_kategorija in table naziv_jela
Here is album of my tables and result of my query with multiple values: http://imgur.com/a/6grPN
Here is album with rest of my tables: http://imgur.com/a/sFPie
Here is my query:
SELECT n.broj_soba, n.id_narudzba, kj.naziv_kategorija, nj.naziv_hrane, us.kolicina,
us.napomena, us.datum_vrijeme, n.izvrseno
FROM narudzba n
JOIN usluga u ON n.id_usluga = u.id_usluga
JOIN usluga_hrana us ON u.id_usluga = us.id_usluga
JOIN naziv_jela nj ON us.id_jela = nj.id_jela
JOIN kategorija_jela kj ON nj.id_kategorija = kj.id_kategorija
GROUP BY n.id_narudzba, us.id_usluga_hrana
I think my query is not good...
you name your table in not-English make us very hard to read/understand/think even though you already translate it to English :)
Your duplicate data can come from your query or your data table
I can see in your usluga_hrana table you have to records of Tomato soup (id=10) with different quantity (4 and 7). That make your query result 2 line of Tomato soup. Another example, You have 4 records of Greek Salad also but because 3 of them get the same quantity=1 so when query run, it return 2 row (quantity 1 and 2)
Not sure what you want, but if you do not want query result duplicate, you should not SELECT the quantity column and do DISTINCT the result if needed