Jump to content

Php Access Different Tables?


Manixat

Recommended Posts

I'm not rly sure if this is possible but what I wanna ask you is how can I get content from a certain table based on a given ID while running a query on another table?

 

select * from `articles` and `username` from `users` where `users.id`=`articles.author` ??

 

I know this can be done in 2 queries but I'm looping a while loop based on the first query and running a second query will result in 1000+ queries and I do not rly want this

Edited by Manixat
Link to comment
Share on other sites

This is what JOINs are for. ;)

Now, it's been a while since I last worked with Access, so the syntax might be a bit off, but the general idea is that you do something like this:

SELECT {$Fields} FROM articles 
INNER JOIN users ON users.id = articles.author

 

If you want to limit this to a subset of the articles, then just attach the correct WHERE clause to that query. ;)

Edited by Christian F.
Link to comment
Share on other sites

This is what JOINs are for. ;)

Now, it's been a while since I last worked with Access, so the syntax might be a bit off, but the general idea is that you do something like this:

SELECT {$Fields} FROM articles
INNER JOIN users ON users.id = articles.author

 

If you want to limit this to a subset of the articles, then just attach the correct WHERE clause to that query. ;)

 

Oh I suppose the title misled you, I'm doing this in MySQL I just meant access literally, anyway I assume I'll google joins now, will post if further trouble occurs

Link to comment
Share on other sites

oh okay I'm a little stuck here, apparently I asked the wrong question first. Ok, in my articles table I have stored the author as a username, which I now figured should turn into an ID and connect it ( relate it ) to the users table, unfortunately I am having trouble converting the usernames into IDs

 

UPDATE `articles` SET `articles`.`author`=`users`.`id` WHERE `articles`.`author` = `users`.`email`

 

This is my query and it says

 

#1054 - Unknown column 'users.email' in 'where clause'

Edited by Manixat
Link to comment
Share on other sites

Okay I somehow managed to turn the usernames to IDs and now I just tried using this

 

 

SELECT users.username, articles.*

FROM `users`

INNER JOIN `articles` ON articles.author = users.id

 

It works out exactly how I need it to, but there are 2 issues:

 

1. I need to have the usernames returned not the IDs

2. it ignores rows from the articles where the ID is 0 which is not supposed to happen

Edited by Manixat
Link to comment
Share on other sites

1. I need to have the usernames returned not the IDs

 

The query I provided should return the username value from the users table and all the fields from the articles table. If that is not happening then you have changed the query from what was provided or the field titled "username" does not, in fact, hold what you think is the username value.

 

2. it ignores rows from the articles where the ID is 0 which is not supposed to happen

That would only happen if there is no record in the users table which has an id of 0. And that IS common. Typically, auto-increment fields start at 1. But, if you do have a record with an ID of 0 the query would work perfectly fine.

 

I'm thinking your problems may be related to this

Okay I somehow managed to turn the usernames to IDs

That seems to me that you weren't positive in the process you did to accomplish that. Could it be you populated the IDs incorrectly?

Link to comment
Share on other sites

By somehow I meant I used a method I'm not completely sure is the right one, but fortunately it worked, which is

 

update `articles` set `author` = (select `id` from `users` where `email`=`articles`.`author`) where (select `email` from `users`) = `author`

 

or something like this, I just recreated it on memory.

 

anyway what I used to get this working is

 

SELECT [all fields from articles without author],`users`.`email` FROM `articles` LEFT JOIN `users` ON `articles`.`author`=`users`.`id`

 

Thank you for your replies! :P

Edited by Manixat
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.