Mysql INNER JOIN and Resource #9 - php

I am working on a UCP(User control panel) where a user loggs in by an unique ID that is given at registration.
My problem is i have two tables in my database:
serverplayers - Holds user ID and name and all of his info
and
vehicles - holds vehicle owner and vehicle model.
So what I need to do is connect those two together by users "Username".I tried Mysql INNER JOIN like this.
$car = mysql_query('SELECT vehicles.vOwner, vehicles.vModel
FROM vehicles
INNER JOIN serverplayers ON vehicles.vOwner = serverplayers.User
WHERE vehicles.vOwner = '.$ro['User'].'');
After this I get Recourse #9, i read that it's not an error it's just that the result is empty?I may have error's or problems in my mysql query so please tell everything whats wrong.
But what's the problem I think is that a vehicle is only connected by it's owner's name and doesn't contain UserID.So what I need is basically show a User what car he has by Connecting his username with vOwner.I'm not a very bright boy when it comes to mysql or php I'm just learning and I came across this thing as mysql INNER JOINS that I think is very usefull(but the main reason is that I don't want to recode the whole server and the UCP).
Vehicles:
ServerPlayers:
ServerPlayers table http://img94.imageshack.us/img94/3369/qebq.png

I'm not sure what $ro['User'] is, but it sounds like an ID based on your question. In that case I would just switch vehicles.vOwner to serverplayers.UserID in that comparison. I would also highly recommend changing your schema so vOwner is the ID and not the username (what if the username changes in one of the tables?) It also doesn't look like you need to join on serverplayers for anything.
The "resource" is a mysql results resource. You can't get anything out of it just by looking at it -- you need to run fetch operations on it:
$row = mysql_fetch_assoc($car);
I recommend using PDO or mysqli over the deprecated ext/mysql.

Related

Error trying to Select columns from different tables in MySql

I can't figure whether to use JOIN or UNION when trying to set this up, but when I found this answer: MySQL Select all columns from one table and some from another table I thought this might work. Turns out I still get an error. Is there something I am doing wrong with setup of this?
$sql = "SELECT user_images.*, users.profile";
Basically I have user images (user_images) in one table and I also want to display some user information such as the profile column in users table.
If you get an error please include it in your question. It's relevant information and you know we're going to ask what it was.
But this is a sample query for what you want to do.
SELECT user_images.*, users.profile FROM user_images JOIN users ON user_images.id = users.id;
Joins need a linking column to work correctly, something that is the same for both rows in both tables. I've used id as an example but it may not be correct for your specific situation.

Mysql - How to get Newsfeed from users you follow

I've been working on this for some hours now and it's getting tiring. I want to get users posts from people I follow and myself, just like twitter does.
So there's a table users_posts that has all users' posts with column user_id to determine who made the post.
Another table users_followers that contains all followers ie. user_id follows following_id
So here's my query:
User ID = 2271
SELECT users_followers.following_id AS following_id, users_posts.id
FROM users_followers, users_posts
WHERE users_posts.user_id = users_followers.following_id
AND (users_followers.user_id =2271)
This query works but the problem is, it's kinda slow. Is there a faster way to do this?
Also, as you can see, I can only get posts from those I follow, and not myself.
Any help?
If I'm understanding your tables properly, I would do this with an explicit JOIN. I'm not sure how much that would speed things up versus the implicit JOIN you're using though.
SELECT following_id, p.id
FROM users_followers f
LEFT JOIN users_posts p
ON (p.user_id = f.following_id)
WHERE f.user_id = 2271
Chances are, adding an index or two to your tables would help.
Using the MySQL EXPLAIN command (just put it in front of your SELECT query) will show the indexes, temporary tables, and other resources that are used for a query. It should give you an idea when one query is faster or more efficient than another.
Your query is fine as written, provided you have properly indexed your tables. At a minimum you need an index on users_posts.user_id and users_followers.following_id.
A query can also be slowed by large numbers of records, even when it is fully indexed. In that case, I find phpmyadmin to be an invaluable tool. From the server page (maybe localhost) select the Status tab to see a wealth of information about how your mysql server is performing and suggestions for how to improve it.

How to write/optimize sql query that need to select data from 6 related tables until it get what it need

I am writing PHP application built on MySQL database made for 5-6 application sharing it. Because of that, I can not alter database structure, and I know many of you will say to do that first, but unfortunately I can't.
Here is my SQL fiddle of database schema, query that I am using, and desired output:
http://sqlfiddle.com/#!2/de7493/1
My solution is working on this example database, but on real production one, where some of these tables have more than 1m rows, when I try to run it my DB crash. Even if I cut down this sql to select only from 3-4 tables it will still crash. Maybe this is not possible to do, maybe I am doing it wrong. Here is what I have to do:
I am dynamically getting cpv_id from url. In my example, cpv_id is 66113000. Based on that value, I have to discover which club offers are related with that cpv_id. Then based on those offers I have to discover which club members are having some of those offers. ( club members are companies ). Then based on club member id, I have to discover some informations about company that is a member of the club, among that data I have to discover company special_id. And based on that special_id I have to read company reports.
So basically: based on cpv_id I have to discover company reports for the company having club offers related to that id ( simple right ? ). As you can see from the way my tables are related in SQLFiddle, I need to get through 6 tables to get what I really need. Once again, I can not alter database structure.
This is very complex thing going on, I am afraid that you will not understand what I need. I hope that SQLFiddle will help. And if you have any more questions please ask me.
So considering that my solution, my query, fail since database crash if I run it. Is there any way to get desired result ? Can I optimize this query somehow, or do I need to write some other one, or do anything else ? I am pretty lost, since I never had to go this deep and read data from so many tables just to get desired result.
Thanks,
Anita
This seems to do the same thing:
SELECT DISTINCT company_report.*
FROM company_report,
company,
users,
club,
club_offer,
club_offer_cpv
WHERE company_report.company_special_id = company.special_id AND
company.id = users.company_id AND
users.id = club.users_id AND
club.id = club_offer.club_id AND
club_offer.id = club_offer_id AND
club_offer_cpv.cpv_id = 66113000
Other people will prefer joins, but I find this easier to read, and they are equivalent. It would look something like this:
SELECT DISTINCT company_report.*
FROM company_report
JOIN company ON company_report.company_special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id AND
club_offer_cpv.cpv_id = 66113000
Actually, that's not bad, I mean I might even prefer this last one.
Add index to your table relationships id's, then try to add one by one table using left outer joins

Getting information from different tables [Best Practice]

I have a MySQL database that I have to get information from, said information is spread across different tables and I have been Googling for a while for the best method to get this information and found quite some information, but I was wondering if there is a best practice that I should try, since the tables may get quite big later on I would like to have a good start regarding functionality, and speed.
If your records in your database are related through ID's or primary keys, you can use the JOIN syntax to get data from multiple tables through 1 query.
Example :
Table car : id, brand
Table driver : id, name, car_id
You can get all the drivers of a car in 1 query:
SELECT * FROM driver LEFT JOIN car ON (car.id = driver.car_id) WHERE car.id=5;
This is just a basic example, but read the MySql documentation (or tutorials) to go on.

Suitable design for a database application

I have a question related to a web app that I developed in PHP, MYSQL.
basically part 1 is :
I display results in the form of table say for software testing.
ID Prod_Name Set Date Result Platform
1 Alpha1 Pro1 01.01.01 PASS 2.3.1.2_OS
Now, I have divided the tables accordingly
Table Name: Results
ID, Name, Date, Result
Table Name : Set
ID, Set_Name, Prod_name
Table Name : Platform
ID, Platform_Name, Set_Name
Now, ID in each table is an incremented value and does not relate to anything else.
My php app, starts with fetching the results from 'Results' table. Since I want SET to be displayed for every row, I am making an another connection to the database and using the query
select Set_name
from Set
where Prod_name = row['Name'] // row['Name'] is fetched from the results table.
now I also want to display platform which I am extracting it from Platform table using the above method i.e making another connection and passing Set_Name = row['Set_Name'] from the Set table.
Now for my application is there any other way to achieve the same result ?
Typically, for large web based applications, if data is coming from a database server is making multiple connection to a DB server a feasible option?
Please do not consider the fact that with MySQL declaring a connection statement once will do the needful but what about MSSQL server? Do we need to write a long sql statement with several joins/selfjoins/unions and use those variables all over the application?
How is the application design for this case will be?
Can anyonce give me some ideas please?
Thanks.
For pretty much any flavour of database, a single SELECT statement which joins three tables will perform better than three separate statements querying a table apiece. Joining is what relational databases do.
I may not have understood everything, but here is something similar. First, let's make an ER model.
Now, because you don't seem to like joins, create a view in the database.
CREATE VIEW v_test AS
SELECT TestID, ProductName, TestName, Date, Result, PlatformName
FROM Product AS p
JOIN Test AS t ON t.ProductID = p.ProductID
JOIN Platform AS f ON f.PlatformID = t.PlatformID;
With this in place, you can simply use:
SELECT * FROM v_test WHERE ProductName = 'Alpha1'
You may also take a look at this question/answer with a similar scenario.

Categories