Questions on SQL Join

Pages: 1, 2
free web hosting
Free Web Hosting > Computers & Tech > Programming > Programming General > SQL (Structured Query Language)

Questions on SQL Join

Ryan
I got this query...

CODE

<?php
error_reporting(E_ALL); //Tell all the things

$host="this_host";
$user="me";
$pass="secret_word";
$users_table="users_table_for_smf";
$member_group_col="column_name_for_member_group_in_smf";
$new_group_name="whatever";
$this_group="the_old_group";
$custom_field="custom_field_in_users_profile";
$this_value="the_value_for_custom_field";

mysql_connect($host,$user,$pass)
   or die ("Can't connect, was told  :  <br>". mysql_error());
mysql_select_db('smf')
   or die ("Can't open database, was told : <br>".mysql_error());

$query = "UPDATE $users_table SET $member_group_col = $new_group_name ";
$query .= "WHERE $member_group_col = $this_group AND ";
$query .= "$custom_field = $this_value ";

mysql_query($query);
print("Updated records: %d\n", mysql_affected_rows());
?>


but i need this modified cause custom profile field data is stored in smf_themes and the membergroup data is store in smf_members with MEMBER ID as the common field, I am hoping someone here can rewrite this for me to work cause I don't know php, i had to get someone to write me this qury but they don't know how toy use the SQL Join.

here is what i want this for: i need a php script to query the db, the smf users table and checks to entires in a users profile a custom field i made and the member group and if both match preset values then i want it to change the member group. i would just want this to be a php file that i can just randomly decide to goto, enter the url in firefox or any browser then it will run.

 

 

 


Reply

miCRoSCoPiC^eaRthLinG
Hey Ryan,
    [/tab]I didn't get a really clear picture from the post, but it seems like you are trying to get a collection of different fields from a set of disjoint tables and depending on the result you intend to modify the column value of a certain table - that's almost what you term as Inner/Outer Join in SQL depending on the tables that you are combining the values from.. There are so many kinds of JOINS (Inner, Outer, Full, Cross, etc.) that if I began to to talk about them the posts would fill up all the space available on any board tongue.gif

[tab]But one factor that is common to almost all the joins is that you have to explicitly specify, WHICH COLUMN is being fetched from WHICH TABLE/DB, i.e., let me show you by modifying a little bit of one of your querries:

QUOTE
$query = "UPDATE $users_table SET $member_group_col = $new_group_name ";
$query .= "WHERE $member_group_col = $this_group AND ";
$query .= "$custom_field = $this_value ";


    [/tab]Say, your $member_group_col resides in one table and $custom_field resides in another and the tables are titled say, A and B, your query would transform into:
QUOTE
$query = "UPDATE $users_table SET A." . " $member_group_col = $new_group_name ";
$query .= "WHERE A." . "$member_group_col = $this_group AND ";
$query .= "B." . "$custom_field = $this_value ";


---> i.e., for joins, columns from different tables (that's what join is for) aren't referred to as simply column names, but they have to be refered as, tablename.columnname in EVERY instance they occur in your query. This is a MUST. Or else, you can you aliasing, wherby you can specify a name, say
Column1 = A.Column1 and
Column2 = B.Column2 --> you can probably see, that these two disjoint columns can be refered in your queries as just Column1 and Column2...

[tab]There's an excellent book on all this named "MySQL CookBook that teaches you all these trick 10 times over in 1000 different ways. It's written by Paul Dubois and published by O'Reilly. You should grab it rightaway. Try eBay -and you might be able to get a second hand one at about 1/10th the price.. I find it an extremely good reference at these confusing points, coz that's where it opens up a handful of really nifty tricks to solve your prob...

    Have fun.. Hope I could help you, even if marginally - coz even I'm relatively new to SQL - I've been messing around with it for at the most an year or so...my main background is in C++ & Java on Linux.. smile.gif

A useful link on JOINS: http://www.sqlteam.com/item.asp?ItemID=11122

 

 

 


Reply

Ryan
right, you lost me their, have you used SMF?

Reply

miCRoSCoPiC^eaRthLinG
Lol.. Ok.. here's the deal. I'm going to grab a copy of SMF and try it out and see if I encounter similar problem. Or else I can try to simulate the exact db/table names that you've set up and try again.. In that case you need to paste here the db and tables names that you were using and a little more details about the table colums..

Reply

Ryan
just look at the stucture to see what type of join i need, cus i want it to if you are in group id 4 lets say and the value of this profile field (stored in themems rater than users) is say yes then i want it to change the group to say groupid 5.

Reply

miCRoSCoPiC^eaRthLinG
    [/tab]While I'm downloading SMF, I'll try make the syntax of the JOINS a little simpler for you:

[tab]Lets say we have two databases DB1 and DB2
And couple of tables in each - but what we have in common are two tables called say, tabxyz - which server different purpose but have the same name in both the databases..

    [/tab]Now, if say, you have changed the active database to DB1. When you do a simple select, say SELECT * FROM tabxyz - it will fetch you the columns from DB1. What IF, you wanted to match one of the columns from the tabxyz of DB2 ??

[tab]In such a case you can:
1. Either switch over and make DB2 your active database - which is a very inconvenient process, coz then you just keep switching between the DBs to everytime you need to fetch values from the other db... OR

2. Use the explicit dbname.tablename declaration to pull values out..In this case, even while your active database is DB1, you can do a select using the following syntax:
SELECT * FROM DB2.tabxyz

    [/tab]Now, lets consider two columns, residing in these tabxyz-s of the two databases... in DB1.tabzyx there's a column called ColA and in DB2.tabxyz, the column name is ColB. You watch to match the values of these two columns and IF they match, you write some value into yet a third column in say, DB2.tabxyz named ColC.. what you do is set up a couple of aliases to refer to ColA and ColB to make life easier for yourself. Thus:

SELECT ColA from DB1.tabxyz AS ColumnA
SELECT ColB from DB1.tabxyz AS ColumnB
UPDATE DB2.tabxyz SET ColC='SomeValue' WHERE ( ColumnA = ColumnB )

[tab] Do you get it now ? ColumnA and ColumnB act as aliases (you can say, almost variables) pointing to ColA and ColB of the corresponding tables.. Thus, when you match the values, you avoid a lot of unnecessary headache of calling up each db and tables - instead you have two very handy aliases ready to be used...

    [/tab] My whole point of writing all this is - that this kind of aliasing syntax is very common in JOINS and you'll encounter them in almost any code that involves JOINS... The process of JOIN itself is what you'd make out from it's name - joining data split over multiple tables and dbs and producing the required output..

[tab] Just keep this syntax in mind and it'll help you a lot in understanding joins.. More on the actual joins later smile.gif

have fun...

Reply

Ryan
ok.. diffrent tables, same db, same prefix. unsure.gif

Reply

Ryan
Thanks for your help but I am still lost, I am going to gte a php scripter to work on this and any other php scripts i will need for my site though.

Reply

vhortex
Using Join

Assume I have this database tables

triwall_data
batch varchar
box int
lot varchar

and

barcode2batch
batch varchar
barcode varchar

All I want to do is get all the records from triwall_data and barcode2batch where the batches matches I will then call this:

CODE

select * from `qa_tracing`.`triwall_data` inner join barcode2batch on triwall_data.batch = barcode2batch.batch limit 0,10


this one is without the join

CODE

select * from `qa_tracing`.`triwall_data`, barcode2batch where triwall_data.batch = barcode2batch.batch limit 0,10


hope this helps...

update functions almost the same..


NOTE: the sample is in assumption that you only want 10 records as is the limit code

Reply

oncombeureum
i think the main idea is here:

QUOTE
$host="this_host";
$user="me";
$pass="secret_word";
$users_table="users_table_for_smf";
$member_group_col="column_name_for_member_group_in_smf";
$new_group_name="whatever";
$this_group="the_old_group";
$custom_field="custom_field_in_users_profile";
$this_value="the_value_for_custom_field";


which part is that can be modified by user ?
is it
QUOTE
$custom_field="custom_field_in_users_profile";
$this_value="the_value_for_custom_field";


your query
QUOTE
$query = "UPDATE $users_table SET $member_group_col = $new_group_name ";
$query .= "WHERE $member_group_col = $this_group AND ";
$query .= "$custom_field = $this_value ";


$query = "UPDATE $users_table SET $member_group_col = $new_group_name "; <- doesn't need to change this part

depending which table is reside in.

QUOTE
here is what i want this for: i need a php script to query the db, the smf users table and checks to entires in a users profile a custom field i made and the member group and if both match preset values then i want it to change the member group. i would just want this to be a php file that i can just randomly decide to goto, enter the url in firefox or any browser then it will run.


what u need is an ID from smf user table that will connect to db and get the field u req.

the query will look something like this.
$query .= "WHERE $member_group_col = $this_group AND ";
$query .= "$custom_field = $this_value ";


continued.

sad.gif
Oncom Beureum
The Best Place in the City

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2
Recent Queries:-
  1. update sql of columns from diffrent tables - 7.56 hr back. (1)
  2. sql 2005 query update join 2 different databases - 68.15 hr back. (1)
  3. join fetch sql - 73.73 hr back. (1)
  4. sql, join chains - 100.42 hr back. (1)
  5. sql joins question - 121.69 hr back. (1)
  6. diffrent joins - 196.14 hr back. (1)
  7. sample sql join questions - 220.40 hr back. (1)
  8. db2 update join example - 222.31 hr back. (1)
  9. sql joins questions - 129.06 hr back. (2)
  10. question and answers on join on sql - 237.65 hr back. (1)
  11. sql join question - 58.43 hr back. (3)
  12. db2 update join set - 247.81 hr back. (1)
  13. sql views questions - 279.18 hr back. (1)
  14. questions on joins in sql - 297.91 hr back. (1)
Similar Topics

Keywords : sql, join

  1. Alluraro
    Come join us (1)
  2. How To Join Two Pages Togother
    (3)
    I made i join link page and and i have a app form that i need to put into that page can anyone help
    on that note....
  3. Activision And Vivendi Join Forces
    to create Activision Blizzard! (5)
    In a whacky and surprising move, Activision and Vivendi S.A had come together in a $18.9
    billion deal to merge Activision and Vivendi Games together to become Activision Blizzard. Both
    companies have been successful with Guitar Hero, Call of Duty and the World of Warcraft series.
    More at Kwokinator.com xboxrulz....
  4. Please Help (php Join Script)
    (5)
    Ok as you all know by now I have been working on a php based game
    to help me learn php. It has been going great and it is almost done. I got some very good help on is
    sues here and along with sites like php.net. However I am stuck and can not find a solution to a
    problem anywere. My Problem: I want users to join but I don't want some charicters to be in
    there name (example I dont want Disallowed Charicters: , ', " I would array like: CODE
    $string = array(<, >, ', "); This for some reason does not work for.
    CODE if(st....
  5. Demonro-revolution, High Rate Quality Online Gaming Experience
    We would like you to join us! (1)
    Dear users, I would like to invite you to come and play on a new High rate Ragnarok Online private
    server. It's name is DemonRO-Revolution, and it is a high quality server, it has experienced
    Support GameMasters and much more. Here is some information about the server: Are you good enough
    to take part in this Revolution? DemonRO Revolution High Rate server, also known as Asylum, is a
    server that fits your tastes. Asylum is a Player Kill server, and constantly updated with the
    newest updates of the emulator. Asylum is very stable, and needs players to grow bigger ....
  6. Fc Foster's: Review My Website
    Visit our website and join to our team (1)
    Fc Foster's ....
  7. Ideas For A Programmers' Association
    JOIN!!! (19)
    Hi, Ev'ryone. I've got a suggestion for the ones that are quite capable af web-programming.
    Let us make an organisation, an association of programmers. The aim of that association will be to
    help eachother while programming. If you're in that organisation, and a problem you couldn't
    solve yourself turns up, you may suddenly leave it on our forum and it will be solved together. To
    join it, Leave here your suggestion. QUOTE 1. My real name. 2. My country. 3. My interests. 4.
    My age. 5. Links to examples of my work. 6. what you are good at. ....
  8. Join World Of Dungeon
    (1)
    Very good text based game. http://world-of-dungeons.net/?link_ea_3469 ....
  9. Free Private Sever For Ragnarok Online
    Join and play for free! (4)
    Hey all, theres a private sever i play on.. total free, 15/15/15 sever, lvl cap 255. GM friendly.
    and some new features being updated right now.. the sever is down for updates but will be back up
    asap. Crimson-RO Hope you all enjoy! -Crimson-Ro Tech Support GM -Full Metal....
  10. Blue Fusion
    Hey join this game! (2)
    yo check this game out http://www.blfusion.com/register.php?ref=1748 ,a rpg exofusion game with
    players, leaders, warriors, and followers. team work and a lone wolf.
    http://www.blfusion.com/register.php?ref=1748 sign up today and become the one. /biggrin.gif'
    border='0' style='vertical-align:middle' alt='biggrin.gif' /> You should also mention that that
    links also gives you some points/other stuff, since it uses a reference number? I think you should
    at least say that. ....
  11. Ipbgaming.com Army System(like Kings Of Chaos)
    Headed up by NilsC, please join... (0)
    Join the IPBGaming.com site, it is run by NilsC, and contains people from both Trap17, Astahost, and
    some people I know from other sites that I beleive followed me... It is a fun game and you will love
    it, try it out... Click Here For IPB Gaming ....
  12. Funky Cool Irc Statistics With Avatars :)
    If this is motivation enough to join IRC (12)
    HI all, For those who join us regularly on IRC and for those who don't as well - now, we have
    hourly updated channel statistics being published at this address:
    http://www.astahost.com/misc/ircstats/astahost.html .. For those who have no idea what channel
    stats mean, just go ahead and take a look at that URL. Also as of today - those who wish to
    include their avatars along with their nicks on the channel stats page, please mail me your avatars
    at: microscopic.earthling@gmail.com. Due to space constraints on the page, I cannot accept avatars
    larger than 80x56....
  13. Whatpulse - Get Pulsing!
    Join the astahost team! (40)
    WhatPulse?!?!?! For a little more community fun please join our WhatPulse team
    http://whatpulse.org/stats/teams/6382/ WhatPulse is a program that counts how many keys and how
    many clicks you use, it does NOT log what you type. It is a little competition and we can be at the
    top! I mean come on look at our member count "2,056" with that we could go far, we could
    definately get in the top 25 but we need your help. We NEED you to join our team. To get whatpulse
    join at there site whatpulse.org and download there client. Its FREE! Then log into t....
  14. Anyone Join My Chain In Age Of Mythology
    newbie or export is ok (0)
    I have the chain in Age of Mythology it is DM_ We always play Deathmatch . If anyone is interesting
    in it , give me the reply and tell me what is your name in ESO . We welcome all of people who
    interesting in Deathmatch !!! Thank You Very Much ~!!....

    1. Looking for sql, join

Searching Video's for sql, join
advertisement




Questions on SQL Join



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE