MYSQL request from two tables, in order to display a table - mysql

I have got a MySQL database, managed with phpMyAdmin.
I am not very clever at MySQL requests.
In table1 are (among others) the 2 following columns :
- id_product
- active
In table2 are (among others) the 2 following columns :
- id_product
- description
I would like to write a request that display a table as follows :
having, at least, the id_product column and the description column
and having only product for which the active field is equal to 1 (the active field can only have a value of 0 or 1)
Thank you in advance for any help in this matter.
Patrick

You want to make an Inner Join between the tables. This matches rows from one table to rows in another, excluding rows that don't have a match. Then you want to add a restriction to only return those with active = 1.
This looks like:
Select t2.id_product, t2.description from table2 t2
inner join table1 t1
on t1.id_product = t2.id_product
where t1.active = 1;

Related

Updating database table based on condition set in another table?

How would I set a condition between 2 tables?
1 table has IDs whose values that need to be changed based on values set in another table.
The query I have so far...no idea if it works.
UPDATE table1 set value='2' INNER JOIN table2 WHERE CONVERT(value USING utf8) LIKE '%text%') so when value is 2 in the first table, that same item in table 2 will be assigned a specific category. The query should check that the ID in table 2 is the same ID that it found in table 1 that contained the value.
Your code looks like MySQL. If so, the correct syntax is more like this:
UPDATE table1 t1 INNER JOIN
table2 t2
ON t1.id = t2.id
set t1.value = '2'
WHERE CONVERT(t1.value USING utf8) LIKE '%text%');
Your question is a bit vague on the actual JOIN conditions, but that is the structure of the query.

MySQL: deleting rows based on a condition with data from another table and NO JOIN

I have a TABLE1 like this:
And a TABLE2 like this:
I want to delete entries from table 1 whose endTimestamp is not equal to ANY table 2 entry endTimestamp, with a margin of 1000 time units.
(I know in this example all the entries from table 1 and table 2 have the same timestamp values, so the 5 entries from table 1 should be kept, and any other should be erased if existed)
Since the ids of both tables are not related to each other, I can't perform a JOIN operation as long as I know.
How can I do this?
EDIT: Tried here. Works, but does not work at my server :|
You're looking for :
delete from table1 where endTimestamp not in (Select endTimestamp from table2)
Edit : As pointed out by #user2864740, you can very well use Join here too, even if the ids of both tables are not related to each other.
DELETE FROM table1 INNER JOIN table2 ON table1.endTimestamp = table2.endTimestamp;

MySQL import column into table that matches key

I've got 2 MySQL tables that I want to merge into one. I've been searching and I couldn't found the way of doing this specific merge:
I want to import a column from table B (key, column) to a new column of table A(key, column1,column2...). This is easy. But only the values of table B that its key is equal to a key of A, and I want it to be in that row.
For example: if I had this 2 tables:
TABLE1 TABLE 2
ID NAME ID TEAM
1 "name1" 1 "bt"
2 "bt2"
I want the result to be:
TABLE1
ID NAME TEAM
1 "name1" "bt"
Thanks!
You would use update with join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.team = t2.team;
However, there really is no reason to do this. You seem to be starting with a normalized data structure, and this is usually a good thing. You can just join in the value wherever you need it, perhaps by creating a view (if you really want).

SQL Update with concatenated data from another table

I'm keep getting lost in this one and struggling to find the right method. Hopefully someone out there might know of a good way of doing what I want to do.
I have two tables and I want to update one table using concatenated data from the other where the ids are the same. As an example....
Table1
ItemID CategoryID
1 20
1 30
1 40
2 10
3 40
3 20
4 10
4 20
Table2
ItemID CatIDs
1
2
3
4
I want to update Table2.CatIDs with all the Category IDs from Table1 where the ItemIDs match. It seems straightforward when I write it down like that but after trying Inner Joins, Sub Queries and so on as I've found online, I keep getting "You have errors in your SQL Syntax..."
I want Table2 to look something like
ItemID CatIDs
1 20,30,40
2 10
3 40,20
4 10,20
I've tried Inner Joins and also sub queries and the closest I've got without an error was this....
UPDATE Table2
SET Table2.CatIDs = Table2.CatIDs + ", " +
(SELECT CategoryID FROM Table1 WHERE Table2.ItemID = Table1.ItemID)
But it doesn't seem finished and all it done was update four rows with the same CatIDs and then give me the message
#1242 - Subquery returns more than 1 row
I'm sure someone out there will be able to see where I'm going wrong and point me in the right direction.
Thanks in advance
You should realize that table1 is the right way to store this information. It is called a junction or association table. Sometimes you need to do the concatenation for presentation purposes, but you need to keep the junction table for full flexibility.
You can do what you want using an update with join and group by:
UPDATE Table2 t2 JOIN
(SELECT t1.ItemId, GROUP_CONCAT(t1.CategoryId SEPARATOR ', ') as cats
FROM table1 t1
GROUP BY t1.ItemId
) tc
ON t2.ItemId = tc.ItemId
SET t2.CatIDs = tc.cats;
Try like below by using group_concat() you can get the , separated list and then join between the tables. But storing comma separated values is never a good idea.
update table2 t2
join
(
select ItemID,group_concat(CategoryID) as newcat
from table1 group by ItemID
) tab on t2.ItemID = tab.ItemID
set t2.CatIDs = tab.newcat

MYSQL - QUERY FROM TWO TABLES

Question - let's say I have 2 tables.
Table 1 - name is permission_list, columns are ID (unique ID), col_ID, user_ID
Table 2 - name is list_entries, Columns are ID (unique ID), title, description, status
I want to select all the rows from table 2 that have status of 'public' as well as all the rows from table 2 that the ID from table 2 shows up in table 1 (under the column col_ID) AND if the user_ID in table 1 matches a certain value. So, anything public, or anything that this specific user has listed under the permissions table. This query would also remove duplicates - in case the user gets a public entry listed in their permissions_list, it wouldn't show up twice.
Hope that makes sense!
Here you go:
SELECT DISTINCT table2.* from table2
LEFT JOIN table1 USING (id)
WHERE status='public'
OR user_ID='someuser';
You need to get some education on JOIN for your first thing, and the second thing is called DISTINCT.
Start here... https://www.google.com/
You have not specified your join condition so we can't give you code samples really. Also the way you worded your question, I'm not entirely sure you don't want a UNION. Read up on those concepts and come back here when you can improve the question.
SELECT table_2.status, table_2.ID, table_1.col_ID
FROM table_1 JOIN table_2
WHERE table_2.status = 'public'
AND table_2.ID = table_1.col_ID
AND table_1.user_ID = 'certain value'
;
Try this