I currently have a couple tables that I'm joining together so I can form all of the relevenat results into a news feed of recent activity. The only problem I'm having is figuring out which table the information is coming from.
$recentActivity = mysql_query("
SELECT *
FROM members
LEFT JOIN market
ON members.id = market.user
LEFT JOIN sales
ON members.id = sales.uid
WHERE members.id='$id'
");
I'm then running a while loop
<? while ($recent = mysql_fetch_assoc($recentActivity)) { ?>
If the result in the loop comes from the market table I would like to be able to echo "market" or something like that and do the same if it comes from the sales table.
Hope this makes sense.
You cannot. The only possible way is to explicitly specify aliases for all (necessary) market table fields. Like:
SELECT members.*,
market.id AS market_id,
market.foobar AS market_foobar
etc. The same with sales table
There's this convention that a lot of people use. If I have a table, Users:
--------------------------------
| user_username | varchar(20) |
| user_email | varchar(40) |
| user_phone | varchar(13) |
---------------------------------
This allows you do have joins and know specifically what table that data came from. It's a sort of namespacing.
If you can rename your fields in the table, I would heavily consider it.
Related
I have a store where I sell products with duration (expiration time for users).
I have a mysql table for users who look like this
| user_id | user_name | user_password | user_email |
and another one for products :
| product_id | product_name | product_price |
I'm selling two products, so now I'm wondering if should I add two columns in user table so it will look like this .
| user_id | user_name | user_password | user_email | product_one | product_two |
and in those fields put the date of expiration for the users who already bought the products (both of them will be blank by default),
or should I just make a new table for the purchased products and then store the appropriate user_id.
Thanks in advance, any help is appreciated.
The second choice is more like a 3NF (3rd Normal Form). I have some little experience in e-shops (mainly in Opencart) and in my opinion, and from whatever I've already seen, this is how they're working.
In fact, it's far better to have one more table which will hold the 'Orders' and a 'User_Id', and another table that will hold the 'Orders' and the 'Product_Ids' in them.
I'm neither a database nor an e-shop platform expert, but according to my experience, I'd go with the second one.
EDIT
I'm editing my current answer to add an example. So, you already have two tables, one for users (customers) and one for products. These two table are (as already mentioned) the following (I don't know the actual table names, so I'll put mine).
table 'users':
| user_id | user_name | user_password | user_email |
table 'products':
| product_id | product_name | product_price |
So, my suggestion is to introduce a new entity (let's name that entity 'order') and create a table that will contains each order matched with the user that made it. So the 'orders' table will be something like this:
| order_id | user_id |
Then you will have another table that will match each order with a product_id. In this table you can have also your 'expiration time' field. A sample of such table is the following:
table 'order_products':
| order_id | product_id | product_exp_date |
However, tha last table has a flaw: it has not a PRIMARY KEY. You have to be a little creative here and import a field in order to hold a primary key, such as order_product_id, which will hold a UNIQUE identifier for each separate product in each separate order. But you'll have to find a way on how to do this.
Hope this clarified my thought.
You want a separate table, which I will name users_products. This will allow you to add products. It's generally more flexible.
It will have these columns
user_id
product_id
expiration
You can find what current products a user possesses like this:
select u.user_name, u.user_email, p.product_name
from users u
left join users_products up on u.user_id = p.user_id
left join products p on up.product_id = p.product_id
and p.expiration >= NOW()
The primary key of your users_products table should be a compound key made of all three columns.
When you sell a user with ID 123 the product with id 321, expiring in 30 days, you represent that in your database with this query.
INSERT INTO users_products
(user_id, product_id, expiration)
VALUES ( 123, 321, NOW() + INTERVAL 30 DAY)
I am using Laravel to creating a website, my users can post questions and other users can write their comments under the post, each comment have Up vote and Down vote, and users can voting for comments.
I need most liked (Up vote) shows topper than others..
This is my database structure and I join them together:
comment table:
comment_id | question_id | user_id | timestamp
up and down votes table (like):
like_id | comment_id | like_type | user_id | timestamp
note:like_type is an enum on mysql and its values are upvote and downvote.
1-What is the mysql query and Laravel codes for that?
2-Is my database Structure right?
1.Calculate SUM belongs to Each Comment
2.Make it order by Desc
select * from (
select ct.comment_id,ct.question_id,
ct.user_id,SUM(case when vt.like_type='upvote' then 1 else -1 end )
cnt from commenttable ct
from votestable
vt left join
on
vt.comment_id=ct.comment_id
)D order by D.cnt desc
I am working on newsfeed and I hope I this right but I want that the image that is uploaded, is linked to the user who uploads it. I have 2 tables.
users in here the accounts are stored.
picas in here the uploaded images are stored.
I have in users a primary key called user_id and I have in the picas table a primary key called id.
I made in picas a new colomn called user_id and when an image is uploaded I tell PHP to get the id from the user who is logged in and then insert that within the user_id colomn from the table picas so I can see which user uploaded the image.
So my users table looks like this:
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| user_id | int(1) | NO | PRI | NONE | AUTO_INCREMENT |
+-------------+-------------+------+-----+---------+----------------+
And my picas table looks like this:
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(1) | NO | PRI | NONE | AUTO_INCREMENT |
+-------------+-------------+------+-----+---------+----------------+
| user_id | int(1) | NO | | NONE | |
+-------------+-------------+------+-----+---------+----------------+
For now I have a problem with the newsfeed. It displays different users with their avatar and names, but if I post something when I'm logged in on an account, it posts for every user. So if I have 5 different users, I log in, I upload an image, then I see 5 posts instant. Check my screenshot here for better display --> http://i.imgur.com/VXwlbra.png
Same goes for the title if you click the image. If I give up a title, then it shows also on the other users.
My code when I upload:
uploadfile.php here I store the uploaded file into the database.
<?php
if (isset($_POST['submit_pica'])) {
$newName = md5(time().$_FILES['pica']['tmp_name']).'.jpeg';
$postTitle = $_POST['postTitle'];
$ses_user = $_SESSION['username'];
$getuserid = mysqli_fetch_assoc($mysqli->query("SELECT id FROM users WHERE username='$ses_user'"));
$userid = $getuserid['id'];
$result = $mysqli->query("INSERT INTO picas (id, name, title, created_at, user_id)
VALUES (null, '$newName', '$postTitle', null, '$userid')");
echo "<script>
$('.upload_button').click(function() {
$('#uploadform').slideUp(300);
});
</script>";
move_uploaded_file($_FILES['pica']['tmp_name'], 'upload/'.$newName);
}
?>
showimages.php (here I output the images from the database.
<?php
$result = $mysqli->query("SELECT *
FROM picas, users
ORDER BY created_at DESC");
$ses_user = $_SESSION['username'];
while($pica = $result->fetch_assoc()) {
echo '<div class="image_post">
<div class="user_avatar"><img src="avatars/'.$pica['username'].'.jpeg" /></div>
<div class="user_name">'.$pica['username'].'</div> <br><br><br>
<div class="image_title">'.$pica['title'].'</div>
<img src="upload/'.$pica['name'].'" />
</div>';
}
?>
My friend says I had to create a foreign key so it created a relationship between the user_id from picas and user_id from users.
Apparently it didn't work. I got the latest version from everything.
I can only choose from: users or picas table (logic, because those are the only ones I have) and then in the option field right next to it, I can only choose id or user_id (depends on which table I choose).
It gives me the this error: http://i.imgur.com/2ukfKsH.png
In the imgur link you see also what I fill in. I go to the table: picas > structure > relation view
It isn't working. I hope using the foreign key is the solution to this problem with the newsfeed, because I want that when the image is uploaded, it outputs the user who uploaded it with the uploaded image and not that it does that + every other user that is stored in the database. Please help me. I struggle with this for already a week. It's driving me crazy!
Thanks for the care you've taken writing this question.
Pro tip: Avoid using SELECT * in queries in software. Using SELECT * allows you to be sloppy in your thinking about what you want in your result set.
Pro tip: If you want results from more than one table, use JOIN directives rather than a comma-separated list of tables. This makes you think clearly about how your tables relate to each other.
You're using this query to display your items.
SELECT * /*wrong!*/
FROM picas, users /*combinatorial explosion!*/
ORDER BY created_at DESC
This query assembles a result set using every possible combination of rows from picas and users. Lucky for you you don't have 50 users and 100 entries in picas yet, or this query would yield 5000 rows.
What you need for your query is something like this:
SELECT users.user_id, users.username,
picas.id, picas.name, picas.title, picas.created_at
FROM users
JOIN picas ON users.user_id = picas.userid
WHERE users.username = '$ses_user'
ORDER BY picas.created_at DESC
This will construct a resultset with the columns I've mentioned. It will use the criterion in the ON clause to join the data from the two tables in a way that makes sense.
I have a comments section on a website i'd like to streamline a bit if possible so it's not as much of an impact on the database. When a user selects a post, and if it has comments associated with it, it lists the comments. when the comments list, it fetches the username from another table. I store the id for the user in the comments table, and use that id to select the record from the users table. and displays as "user" said:
lets say i have 1000 comments on a post, it will hit the users table 1000 times to grab user names. I think this is probably a bad design. i thought of a few solutions, but don't know what would be recommended in this situation.
should i just be storing the username inside the comments table?
should i store all of the usernames already called in a session array?
put all of the usernames in a file, and call from the file?
or is there another solution that i haven't thought of?
i'm kind of confused. I thought i was doing the right thing by using the IDs in the comment table, and then using it to fetch the username, but after reading about a million posts on using less impact on the database, i'm starting to question myself.
WOW, thanks for all of the useful answers. here is the table scheme, i don't know why i didn't put in in originally.
comments table for jokes:
id | author_id | joke_id | date_created | body
---+-----------+---------+--------------+-----
1 | 3 | 2 | 2011-06-12 | this is a comment
and for the users:
id | user_name | password | email | date_joined | visible
---+-----------+----------+-------+-------------+---------
3 | booboo | password | email | todays_date | 1
This is what JOINs are for - so that you can run a single query and efficiently get the combined information from multiple tables. E.g.:
SELECT comments.id, comments.content, users.name
FROM comments
JOIN users ON comments.user = users.id
WHERE comments.id in (1,2,3)
would look up the 3 comments with id 1, 2, and 3, plus also get the username of each commenter, and return rows that looked like this:
comments.id | comments.content | users.name
------------+-------------------+---------
1 | "First comment." | "Poster1"
2 | "Second comment." | "Poster2"
3 | "Third comment." | "Poster3"
It sounds like you have a userID field in your comments table, but need to look up the username, correct? If so, a JOIN would be the best solution.
Something like:
SELECT *
FROM `comments`
LEFT JOIN `users` ON `users`.`id` = `comments`.`userid`
WHERE `postid`='1'
To read more on joins and their endless possibilities, read up here
SELECT * FROM comments LEFT JOIN users ON comments.posterid = users.id
Google for LEFT JOIN for more info :)
Do you allow the same username to be used more than once? If not, then I would use the username field as the PK of your users table and store that in the commentstable as the FK. That'll solve your issue nicely.
If changing the PK of your users table is too much of an issue, then just store the username in the comments section since you can still use that select a single record from your users table.
I have many a times tried using nested query for MySQL in PHP, but it does not work. Is it not possible to do nested/Joins queries?
Just a Scenario:
I have two tables one table with user id and the other with data. User logins and with sessions I have to cross check two different tables with user id (user and data). Is it not possible to nest/join these two tables to write a single query statement.
In short is nesting or joining two or more tables permitted in PHP coding?
YES, it is possible to join two or more tables in MySQL (and therefore, also when using PHP).
You need to post your table schema, if you want us to show a relevant join query. You could, however, try something like:
SELECT * FROM user AS t1
CROSS JOIN data AS t2
ON t1.userid=t2.userid
WHERE t1.userid='154'
(This query presumes that there always will be one row with the userid in both tables. You should use LEFT JOIN instead of CROSS JOIN to return a row even if there is no row in data for the userid. 154 is just an example userid.)
Have a look at http://dev.mysql.com/doc/refman/5.5/en/join.html for information on the JOIN syntax.
users
| user_id | username | password | enabled |
|---------|----------|----------|---------|
| 1 | john | sgsd2gg | 1 |
| 2 | jane | sdshdhd | 0 |
users_data
|udata_id| user_id | some_column |
|--------|---------|-------------------|
| 1 | 1 | Some title |
| 2 | 2 | another title |
Since you haven't posted your table schema, I can't give you an exact solution. But supposing you have a users table and a users_data table, where users_data are owned by a user. You can do a join on the table to retrieve all the data.
SELECT * -- Don't select all fields unless you need it
FROM users U LEFT JOIN users_data UD ON U.user_id = UD.user_id
WHERE U.user_id = 1
This would pull all the records for user with an ID of 1. This is a very simplistic join, but it should give you an idea.
Here's an example that visually describes the different options you can use : SQL Join Differences