MySQL (Get 2 tables in 1 command) - mysql

I want to know how to connect MySQL Tables I will give a example so you guys can understand my question.
Page Table
(Use to echo the NavBar Items and )
╔════╦══════════════╦═══════╦═════════════════╗
║ ID ║ Name ║ Type ║ Background Image║
╠════╬══════════════╬═══════╬═════════════════╣
║ 1 ║ Home ║ home ║ --------------- ║
║ 2 ║ AboutUs ║ blank ║---------------- ║
║ 3 ║ Services ║ slider║--------------- ║
║ 4 ║ Contacts ║ blank ║---------------- ║
╚════╩══════════════╩═══════╩═════════════════╝
But there is a thing, we can have multiple pages with same type of page (blank,sldier ect...)
At slider we have 2 tables with information.
Slider Settings
(Use to control the number of slider on a certain ID page)
╔════╦══════════════╗
║ ID ║ Num_Slides ║
╠════╬══════════════╬
║ 3 ║ 4 ║
╚════╩══════════════╝
I want to know if theres anyway to connect this db (not physicly)

Are you trying to do just a join, something like this?
select * from page_table, slider_settings where page_table.id = slider_settings.id and page_table.type='slider';

Related

Pivoted MySQL table: Can I group groups and avoid results in one row?

I'm having trouble pivoting a table in MySQL. I've been reading trying to accomplish the result I want, and I'm almost there, but I can't see clearly what to do next.
I'm creating a report of a questionnaire. The report states the person who applied the questionare, the one who solved it and the set of questions with the answers given and some more info.
I have already a query that gives the correct info this way for every person:
╔═══════════════╦══════╦══════════════╦═══════════╦════════════╦════════════════╗
║ supervisor ║ zone ║ promoter ║ visit_id ║ question ║ answer ║
╠═══════════════╬══════╬══════════════╬═══════════╬════════════╬════════════════╣
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ Question A ║ 1 ║
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ Question B ║ 1 ║
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ Question C ║ Any given text ║
╚═══════════════╩══════╩══════════════╩═══════════╩════════════╩════════════════╝
Using GROUP_CONCAT and CONCAT as seen in many tutorials, I've managed to pivot the report retrieving the correct info.
In a nutshell, I did it like this:
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(IF(infoBase.question = \'',question,'\', \'',answer,'\', 0)) AS \'',question,'\'',''), "\n"
) INTO #answers
FROM (many tables)
WHERE (some expresions)
Then I use a prepared statement to get the final result like this:
SET #query :=
CONCAT(
'SELECT
infoBase.supervisor,
infoBase.zone,
infoBase.promoter,
infoBase.visit_id,
',#answers,' FROM (same tables as before) WHERE (same expresions as before)'
);
Then I execute the query:
PREPARE statement FROM #query;
EXECUTE statement;
Whilst I retrieve the info correctly, my result set looks like this:
╔═══════════════╦══════╦══════════════╦═══════════╦════════════╦════════════╦════════════════╦════════════╦════════════╦══════════════════════════════════════════════════════════════════════╦════════════╦════════════╦════════════════════════════╗
║ supervisor ║ zone ║ promoter ║ visit_id ║ Question A ║ Question B ║ Question C ║ Question A ║ Question B ║ Question C ║ Question A ║ Question B ║ Question C ║
╠═══════════════╬══════╬══════════════╬═══════════╬════════════╬════════════╬════════════════╬════════════╬════════════╬══════════════════════════════════════════════════════════════════════╬════════════╬════════════╬════════════════════════════╣
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ 1 ║ 1 ║ Any given text ║ 3 ║ 3 ║ Including punctuation characters, like comma, admiration ! or dots . ║ 2 ║ 2 ║ Text is fun, text is life! ║
╚═══════════════╩══════╩══════════════╩═══════════╩════════════╩════════════╩════════════════╩════════════╩════════════╩══════════════════════════════════════════════════════════════════════╩════════════╩════════════╩════════════════════════════╝
The info is correct but it's in the same row and the first part about the people is not seen. So I try to group by visit_id that gives unicity, and I get this:
╔═════════════════╦══════╦══════════════╦═══════════╦════════════╦════════════╦════════════════╗
║ supervisor ║ zone ║ promoter ║ visit_id ║ Question A ║ Question B ║ Question C ║
╠═════════════════╬══════╬══════════════╬═══════════╬════════════╬════════════╬════════════════╣
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ 1 ║ 1 ║ Any given text ║
║ NEYRA OLIVER ║ VDM2 ║ OMAR SOTO ║ 114442 ║ 1 ║ 1 ║ Any given text ║
║ KAYRA RODRIGUEZ ║ GU1 ║ VICTOR SOTO ║ 114197 ║ 1 ║ 1 ║ Any given text ║
╚═════════════════╩══════╩══════════════╩═══════════╩════════════╩════════════╩════════════════╝
Now the first part of the table is correct but the second part about the questions just reflects the results of the first person in every row. I need to get the following in order to succeed:
╔═════════════════╦══════╦══════════════╦═══════════╦════════════╦════════════╦══════════════════════════════════════════════════════════════════════╗
║ supervisor ║ zone ║ promoter ║ visit_id ║ Question A ║ Question B ║ Question C ║
╠═════════════════╬══════╬══════════════╬═══════════╬════════════╬════════════╬══════════════════════════════════════════════════════════════════════╣
║ EDGAR MONTIEL ║ VM1 ║ MIGUEL LOPEZ ║ 104285 ║ 1 ║ 1 ║ Any given text ║
║ NEYRA OLIVER ║ VDM2 ║ OMAR SOTO ║ 114442 ║ 3 ║ 3 ║ Including punctuation characters, like comma, admiration ! or dots . ║
║ KAYRA RODRIGUEZ ║ GU1 ║ VICTOR SOTO ║ 114197 ║ 2 ║ 2 ║ Text is fun, text is life! ║
╚═════════════════╩══════╩══════════════╩═══════════╩════════════╩════════════╩══════════════════════════════════════════════════════════════════════╝
This is an over simplification of the real problem. The whole problem involves tens of questions for differents questionnaires. Say questionnaire 1 has 45 questions, questionnaire 2 has 30 questions, questionare 3 has 80 questions, etc. So I don't even know the exact number of questions as it varies, thus I'm trying to get this dynamically.
How can I get this result? What do I need to group by or what do I need to do?

Retrieve Hierarchical Data from MySql database [duplicate]

This question already has answers here:
How to do the Recursive SELECT query in MySQL?
(6 answers)
Closed 6 years ago.
This is my Mysql Database
╔════╦═══════════╗
║ ID ║ Parent_ID ║
╠════╬═══════════╣
║ 1 ║ 0 ║
╠════╬═══════════╣
║ 2 ║ 1 ║
╠════╬═══════════╣
║ 3 ║ 1 ║
╠════╬═══════════╣
║ 4 ║ 3 ║
╠════╬═══════════╣
║ 5 ║ 4 ║
╚════╩═══════════╝
What I want to achieve :
When user search for ID 1 , I want to get all those element whose Parent_ID is 1 and also all those IDs whose have 1 as their parent or grand parent or great grand parent and so on.
OR Simply all descendants of ID 1.
Example :
if user search for 1, the algorithm should give result
{2,3,4,5} -- 4,5 because 1 is their great grand parent.
if user search for 2, the algorithm should give empty result { } as no
element have 2 as a Parent_ID
if user search for 3, the algorithm should give result {4,5} -- 5 because 3 is its grand parent.
What is the good way of saving and retrieving these type of data from database?
I am using Java and MySQL.
Thanks.
Seeing your data model, you can't do it in a single SQL query, since you need an unknown level of recursion. You need to create a function or stored procedure to iterate over the results of subsequent queries.
This answer may help you:
https://dba.stackexchange.com/questions/30021/mysql-tree-hierarchical-query
[Edit]
If you always query for a root parent (i.e. a row that may have children but has no parent), you can store that id in each row and query by that column:
╔════╦═══════════╦════════════════╗
║ ID ║ Parent_ID ║ Root_Parent_ID ║
╠════╬═══════════╬════════════════╣
║ 1 ║ 0 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 2 ║ 1 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 3 ║ 1 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 4 ║ 3 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 5 ║ 4 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 6 ║ 0 ║ 6 ║
╠════╬═══════════╬════════════════╣
║ 7 ║ 6 ║ 6 ║
╠════╬═══════════╬════════════════╣
║ 8 ║ 7 ║ 6 ║
╚════╩═══════════╩════════════════╝
Then you can just query:
SELECT * FROM mytable WHERE Root_Parent_ID = 1
Otherwise, I recommend you to go more in depth on how to store this data more efficiently: https://blogs.msdn.microsoft.com/anthonybloesch/2006/02/15/hierarchies-trees-in-sql-server-2005/

SQL My Sub Query is Loading Forever

Okay so basically I am trying to run a simple query with a subquery on phpmyadmin using mysql and it won't stop loading after I run it. The query is:
SELECT t.tagValue FROM tags t WHERE t.tagID IN (SELECT ua.tagID FROM user_taggedArtists ua WHERE ua.userID = 2);
I have ran the individual queries on their own without combining them together and they seem to do what I want, but when I mix them into a subquery form phpmyadmin just loads forever, like I am getting an infinite loop or something.
tags table looks like that:
╔═══════╦═════════════╗
║ tagID ║ tagValue ║
╠═══════╬═════════════╣
║ 1 ║ metal ║
║ 2 ║ alternative ║
║ 3 ║ pop ║
╚═══════╩═════════════╝
etc.
user_taggedArtists table looks like this:
╔════════╦══════════╦═══════╦═════╦═══════╦═══════╗
║ userID ║ artistID ║ tagID ║ day ║ month ║ year ║
╠════════╬══════════╬═══════╬═════╬═══════╬═══════╣
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
╚════════╩══════════╩═══════╩═════╩═══════╩═══════╝
ect.
Not sure what I am doing wrong here and any help would be greatly appreciated.
Thank you!
Hard to say 'zatly, but "IN", generally, is slow. Try a JOIN and WHERE. I'm going to pretend we can join on tagID. You should have indexes on the join column(s). If not anything you do will perform poorly.
SELECT t.tagValue
FROM tags t
INNER JOIN user_taggedArtists ua ON
t.tagID = ua.tagID
WHERE ua.userID = 2

MySql: Consolidating rows with matching records

My table has a list of users with Items associated to them. I hope to be able to consolidate the items field into a list or create another column if the row has a matching User/email.
I have the following structure with in the MySQL table:
╔═══════════╦═════════════╦═════════════╦═════════════╦═════════════╦════════════╗
║ ID ║ User_ID ║ Item ║ Date ║ User ║ Email ║
╠═══════════╬═════════════╬═════════════╬═════════════╬═════════════╬════════════╣
║ 1 ║ 1 ║ Laptop 1 ║ 30th Nov ║ John ║ J#test.com ║
║ 2 ║ 2 ║ Laptop 3 ║ 12th Nov ║ Emma ║ e#test.com ║
║ 3 ║ 2 ║ Camera 3 ║ 12th Nov ║ Emma ║ e#test.com ║
╚═══════════╩═════════════╩═════════════╩═════════════╩═════════════╩════════════╝
I am very new to SQL but i think i would need to use some type of transpose field to a column function?
Any help will be greatly appreciated
Thanks
I think you want group_concat(). Personally, I would use user_id, rather than user/email. or, perhaps all three together. Something like this:
select user_id, user, email, group_concat(item) as items
from table t
group by user_id, user, email;

Update a table by inserting a count of foreign key from another table

I have two tables:
╔════════════════╗ ╔════════════════╗
║ ITEM ║ ║ ITEM_TRACK ║
╠════════════════╣ ╠════════════════╣
║ ID ║ ║ ID ║
║ GUID ║ ║ ITEM_GUID ║
║ COUNT1 ║ ║ CONTEXT ║
║ ENDDATE ║ ║ ║
╚════════════════╝ ╚════════════════╝
╔═════╦══════╦════════╗ ╔═════╦═══════════╦══════════╗
║ ID ║ GUID ║ COUNT1 ║ ║ ID ║ ITEM_GUID ║ CONTEXT ║
╠═════╬══════╬════════╣ ╠═════╬═══════════╬══════════╣
║ 1 ║ aaa ║ ║ ║ 1 ║ abc ║ ITEM ║
║ 2 ║ bbb ║ ║ ║ 2 ║ aaa ║ PAGE ║
║ 3 ║ ccc ║ ║ ║ 3 ║ bbb ║ ITEM ║
║ 4 ║ abc ║ ║ ║ 4 ║ ccc ║ ITEM ║
╚═════╩══════╩════════╝ ║ 5 ║ abc ║ ITEM ║
║ 6 ║ aaa ║ ITEM ║
║ 7 ║ abc ║ ITEM ║
║ 8 ║ ccc ║ PAGE ║
╚═════╩═══════════╩══════════╝
What I'm trying to do is fill in the COUNT1 column in ITEM with the count of the number of times ITEM_GUID appears in ITEM_TRACK for all ITEM.GUIDs where ENDDATE is still in the future. I need to do this once an hour for all GUIDS in ITEM.
I can get the counts I need easily
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
What I don't know how to do is, how do I merge this with an INSERT INTO statement to automatically update all the items in the items table with the count based on their ENDDATE?
UPDATE:
I have a working solution based on Aquillo's answer:
UPDATE ITEM a
SET COUNT1 = (SELECT COUNT(*) AS total FROM ITEM_TRACK b WHERE b.item_guid=a.guid);
Is there any other way to do this without a subquery?
You can insert from a select like this:
INSERT INTO myTable (foreignKey, countColumn) VALUES
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
In case you want to update, try something like this:
UPDATE from SELECT using SQL Server
If you use INSERT INTO you'll put additional rows in your ITEM table, not update the existing ones. If this is what you meant then that's great, but if you want to update the existing ones, you'll need to use update. You do this by joining the table you want to update with the table you want to update from. However, in your case you want to update from an aggregation and so you need to create a table with the aggregated values. Try this:
UPDATE ITEM SET Count1 = temp.total
FROM Item
INNER JOIN (
SELECT ITEM_GUID, COUNT(*) AS total
FROM ITEM_TRACK
GROUP BY ID) AS temp
ON Item.GUID = temp.ITEM_GUID
WHERE ENDDATE > NOW()
I've tried this on SQL Server (using GETDATE() instead of NOW()) to double check and it worked, I think it should work on MYSQL.