|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
MySQL 5 - Is this a Case For JOIN?
just trying to improve my code some more and looking it over i got to think JOIN might work but im not sure. Here's a sample
PHP Code:
and so on? So would a JOIN be more useful. I'm using shared ID in table 1 to locate info in table 2 to and then post that info. But I am also posting info from table 1 in the same record as the table 2 id
__________________
29 years of creative writing 13 years of HTML 10 years of Photoshop 6 years of PHP/MySQL And I never knew Photoshop could do HTML until 2004! You learn something new every day. |
|
#2
|
|||
|
|||
|
a join could help:
Code:
SELECT *
FROM Content
JOIN Bylines
ON Content.ByLineID=ByLines.BylineID
|
|
#3
|
|||
|
|||
|
Quote:
but would i still be able to pull info from the byline table as well? what if i have multiple IDS, one of my tables shares ids with 2 other tables? |
|
#4
|
|||
|
|||
|
that query joins both tables together. I suggest just running it in a phpmyadmin query window to check out the results. you can also join in as many tables as you want by just copying the JOIN tableName ON tableName.commonColumn=table1.commonColumn
|
|
#5
|
|||
|
|||
|
Quote:
so say something like this PHP Code:
could be simplified to PHP Code:
which wound create one array (instead of 6) with the combined data of all six tables? What if the multiple tables share field names, will their be a conflict when i try and reference $ContentArray['Name'] if say Menu, content, and issue all have a Name Field? |
|
#6
|
|||
|
|||
|
more like:
Code:
SELECT *
FROM Content
JOIN Bylines
ON Content.ByLineID=ByLines.BylineID
JOIN Issues
ON Content.IssueID=Issues.IssueID
JOIN Category
ON Content.CategoryID=Category.CategoryID
JOIN Menu
ON Content.MenuID=Menu.MenuID
WHERE 1=1
Just know that if there are any tables that have more than one row, you will get seemingly duplicate data. you can do a join on more than one column if you have more to join the tables on. like I said before, run it in phpmyadmin to check the results and see if you get what you want back. It has to do with how the tables are connected, it kinda gets confusing, but there are some good tutorials around. w3schools has one (http://www.w3schools.com/Sql/sql_join.asp) and although I haven't looked at this specific tutorial, I usually find their tutorials of high quality. Also so you know, most people who write sql for mysql use a non-standard join. the above join (at least the one I most commonly write) is considered an ANSI join. they are considered to be better practice, but since most people who learn sql for mysql learn the non-standard way, you will most commonly see the non-standard (so if it looks different than most queries you see, thats why). |
|
#7
|
|||
|
|||
|
yeah their will be more then one row so maybe join isnt the best way? for example in my content example you will have several bylines in the byline row but your really only going to have 1 byline per content (blog/article/what have you)
But content could be in multiple menus or categories... |
|
#8
|
|||
|
|||
|
a join might be the right way to go, you would just have to work with the results. like I said above, make the join query and run it in phpmyadmin to see what the results are like (if you gave all the right queries in your attempts, then the one I wrote should work). also check out that w3schools link in the previous post for a better idea of how joins work.
|
|
#9
|
|||
|
|||
|
thanks I'll do that once i have some data right now I'm still building the infrastructure so my database is empty.
|
|
#10
|
|||
|
|||
|
if you want/need any help come time to setup some of those queries, feel free to PM me (and I can get you my email if you want) and I can help. at that time you could either get me access to the database (phpmyadmin/other admin tool/custom write a query, show a table tool) or you can send me a copy of the database layout and some random data (http://generatedata.com) to test.
|
|
#11
|
|||
|
|||
|
i'll keep that in mind. Thanks!
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > MySQL 5 - Is this a Case For JOIN? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|