MySQL Query with a status check from another table - mysql

There are three tables involved with this query.
Table1:
app_id
app_name
menu_id
1
BigApp
1
2
smallApp
2
3
theApp
2
Table2:
menu_id
menu_title
1
menu1
2
menu2
Table3:
user_id
app_id_list
1
1,2,3,4,5
2
1,3,5
So I want to grab the app_name, menu_title and then add another column (user_status, where would 1=on, 0=off) to verify that the app_id shows up in the app_id_list for a given user. The results for user_id = 2 would be:
app_name
menu_title
user_status
BigApp
menu1
1
smallApp
menu2
0
theApp
menu2
1
The SQL statement I've got so far is:
SELECT Table1.app_name, t2.menu_title
FROM Table1
INNER JOIN Table2 AS t2 ON (t2.menu_id = Table1.menu_id)
Not sure how to find the last column of data from Table3. Any thoughts?

After reviewing the following discussion:
Is storing a delimited list in a database column really that bad?
I have solved this issue by creating and expanding the Table3 where each value that was in the app_id_list now has its own row. Faster and can just link up with another JOIN.

Related

find the row which has the highest sequence number in a table for a specific ID that is in another Table

I'm have 2 tables:
Table 1:
Job #
XLG24
ABC123
XYZ123
Table 2:
Sequence # Job # Display Order
1 XLG24 1
2 XLG24 2
2 ABC123 1
3 ABC123 2
4 ABC123 3
4 XYZ123 1
I am trying to pull the Job # and Sequence # that are found in Table 1 from Table 2. however, when i try it only pulls the job #s found in Table 1 that have the highest sequence # (in this case only Sequence # 4).
This is my code:
select * from TABLE2 where JOB# IN
(SELECT JOB# FROM table1)
and SEQUENCE# = (select max (SEQUENCE#) from Table2)
order by Display_order desc
The output of my code is like this:
Sequence # Job #
4 ABC123
4 XYZ123
I want it to look like:
Sequence # Job #
2 XLG24
4 ABC123
4 XYZ123
So in summary, I am trying to only find the row which has the highest sequence number in Table 2 for a specific ID that is in Table 1
Can someone please give any advice?
Assuming table1 has unique entries (job number only shows up once), and table2 has multiple entries (I'm sure these are both just example names for this post), then I think you want to join table1 to table2 and then find the max.. like so:
SELECT t1.job# jobNum,max(t2.Sequence#) maxSeq
FROM table1 t1 LEFT JOIN table2 t2 ON t1.job#=t2.job#
This will include all entries in table1 - even if they're not found in table2 - if not found in table2 then maxSeq will contain NULL. If all entries are in both tables, or you only want all entries found in both, you'll want an INNER JOIN instead.

Fetching crossed records from database

I have the following table structure (simplified):
id | structure_id | structure_hash_id
1 1 1
2 1 2
3 1 3
4 2 4
5 2 1
6 3 2
As you can see, all structures contain many structure hashes. What I want to fetch is information for each structure id, how many structure hashes it contains exist in other structures. So for this example it'd be:
structure_id #1: 2
structure_id #2: 1
structure_id #3: 1
The query I wrote for this is:
SELECT contains.structure_id, COUNT(contains.structure_hash_id)
FROM (
SELECT *
FROM structureTable st
WHERE structure_id = 1
) AS contains
INNER JOIN (
SELECT *
FROM structureTable st
WHERE structure_id != 1
) AS notcontains
ON contains.structure_hash_id = notcontains.structure_hash_id
GROUP BY contains.structure_id;
It works, I wrote it from memory, I don't remember how I wrote it earlier as I deleted it, but you got the idea.
But the problem is that in real table I've got ~500mln records and some other columns, so for each structure_id the query execution time is huge (> 15min).
Also, I have type structure_id manually, while I'd like to have them all as a result like I gave an example at the top of this post.
How can I solve this problem?
You can achieve this with self join and group by.
Here is the way to do that:
select
t1.structure_id ,
count(t1.structure_id ) as count
from structure t1
inner join structure t2 on t1.structure_id !=t2.structure_id
and t1.structure_hash_id=t2.structure_hash_id
group by t1.structure_id
SQL Fiddle Example: http://sqlfiddle.com/#!9/678bf7/1/0

Join from two tables mysql

I have a doubt where trying to join two tables by a previous search. I've looked several solutions and read some chapters in a mysql book but I think I'm pretty close to the right answer but still not get it
I have this table "userprocess":
idProcess username state
------------------------------------------
1 blisssing 3
2 enriquecalvera 1
2 africabaluja 2
1 enriquecalvera 3
2 blisssing 1
The primery key for this table is the union of idProceso+username.
I have this other table "user":
index username pass active tipeUser .... so on
----------------------------------------------------------------- ----
1 blisssing 6OiZVVUPi3LDE 1 user
2 carmen 6OOtfrXB2Nu5. 1 user
3 consuelo 6OgdhVSkr1VDs 1 user
4 africabaluja 6OoPtGjWMQARE 1 user
5 enriquecalvera 6O6tvHg.122uQ 1 user
The thing is I want to show the join of the two tables but with a search within the first table. If I run this query
SELECT username FROM userprocess where idProcess='1' ORDER BY state
I get this:
username
---------
blisssing
enriquecalvera
which is what I am looking for, but I want to show all the fields in the "user" table for those usernames ordered by idProceso. So I run this other query:
SELECT *
FROM
user u,
userprocess p
WHERE
u.username=p.username
AND u.username IN (
SELECT username
FROM userprocess
where idProcess='1'
ORDER BY username
) ORDER BY p.state
I got this:
username pass active tipeUser idProcess state
----------------------------------------------------------------------
blisssing 6Od3nSkfOiwlg 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 1 3
blisssing 6Od3nSkfOiwlg 1 user 2 3
But this is not what I want I just want the same two results as in the previous query but with all the columns of the result of joining the two tables..
I know there is a lot of questions like this, but I have tried a lot of things and still not having the desire result..
What am I missing?
thank you, if you have any qestion or doubt just ask :)
The reason you're seeing multiple results is because you're joining on just the username, but of course the userprocess table has 2 rows where username = enriquecalvera. Your subquery is correctly only returning the 1 row you're interested in (where idprocess = 1) but as your join is seperate to this, and therefore doesn't include the idprocess = 1 condition, you're getting both rows back.
You should just do this in one step with a join like this:
SELECT *
FROM
user u
INNER JOIN userprocess p on u.username=p.username and p.idProcess='1'
ORDER BY p.state

Quickest way to combine two identical structured tables without duplicate entries

I have two tables with the exact same structure/columns. I need to combine them without duplicates based on the second column (title). First column is ID and these may have duplicates but should be ignored.
Example of database structure
id (primary key, auto increment), title (unique), description, link
Ex. Table 1
1 Bob thisisbob bob.com
2 Tom thisistom tom.com
3 Chad thisischad chad.com
Ex. Table 2
1 Chris thisischris chris.com
2 Chad thisischad chad.com
3 Dough thisisdough doug.com
What I need in Table 3
1 Bob thisisbob bob.com
2 Tom thisistom tom.com
3 Chad thisischad chad.com
4 Chris thisischris chris.com
5 Dough thisisdough doug.com
Both tables have about 5 million entries/rows each. I need the most efficient way possible to combine them.
It is a little hard to understand exactly what you want. Perhaps, though, this might be:
select *
from table1
union all
select *
from table2
where not exists (select 1 from table1 where table1.title = table2.title);
This will run faster with an index on table1(title).
EDIT:
If you want to insert them into a third table, you can do:
create table table3 as
select *
from table1
union all
select *
from table2
where not exists (select 1 from table1 where table1.title = table2.title);

Updating one table row with information from multiple rows in different table

I have searched quite a lot but haven´t found a solution to my problem. Now I hope someone can help me here.
I have two tables in an MySQL database and both tables have the columns Name and ArticleID
What I would like to do is to copy the content in the column Name from Table 1 to Table 2 where the ArticleID match. All the names should be separated by a comma in the column Name in Table 2.
In Table 1 there are for example 3 rows with the same content in the column Name but each row has a unique ArticleID. In 3 other rows there are a different Name but the ArticleID´s is the same as the first 3 rows.
Table 1
Name 1 - 1
Name 2 - 2
Name 3 - 3
Name 4 - 1
Name 5 - 2
Name 6 - 3
Table 2
1 - Name 1, Name 4
2 - Name 2, Name 5
3 - Name 3, Name 6
This would not normally be a problem for me, But now there are multiple rows with the same ArticleID and i can´t seem to figure it out by my self.
I hope you understand what I want :-)
Melker
INSERT INTO TABLE2(id, names)
SELECT ArticleID, GROUP_CONCAT(Name)
FROM TABLE1 GROUP BY ArticleID;
UPDATE
TABLE2
JOIN (SELECT ArticleID, GROUP_CONCAT(Name) AS Name FROM TABLE1 GROUP BY ArticleID) TABLE1
ON TABLE2.ArticleID = TABLE1.ArticleID
SET TABLE2.Name = TABLE1.Name
WHERE TABLE2.ArticleID = TABLE1.ArticleID
I actually had to use UPDATE and this worked