php - How to output correct database row

Solution:
Your query should be written this way:
SELECT
u.id userid,
u.username username,
u.alliance allyid,
SUM(v.pop ) AS totalpop,
SUM(a.Aap ) AS totalpoint,
COUNT(ua.alliance ) AS totalusers
FROM users u
INNER JOIN vdata v ON v.owner = u.userid
INNER JOIN alidata a ON a.id = u.allyid
INNER JOIN users ua ON ua.alliance = u.allyid
WHERE users.alliance > 0
GROUP BY u.id,
u.username,
u.alliance
ORDER BY u.totalpoint DESC,
u.allyid ASC ;
What I have done in this query:
IJOIN
ed the tablesusers
,vdata
,alidata
andusers
again instead of these correlated subqueries that you used in your question. Note that: I joined the tableusers
one more times to get those users that hasalliance = u.allyid
but with a different alias. ThenGROUP BY
, with aggregate functions in the same query.
You might also need to useLEFT JOIN
instead ofINNER JOIN
in case you want to include those unmtached data, i.e those users that has no entries in the others tables for example.
Update 1
To test the query against MySQL directly, and since you are using WAMP. Like in the following steps:
- Ensure that the WAMP server is running:
- From the task par open the phpMyAdmin:
- Select your database that you're working with:
- Navigate to SQL Tab:
- Paste your query in the window:
- Then press the button Go.
Update 1
Try this instead:
SELECT
u.id AS userid,
u.username AS username,
u.{-code-18} AS {-code-14},
SUM(v.pop) AS totalpop,
SUM(a.Aap) AS totalpoint,
COUNT(ua.{-code-18}) AS totalusers
FROM s1_users u
LEFT JOIN s1_vdata v ON v.owner = u.id
LEFT JOIN s1_alidata a ON a.id = u.{-code-18}
LEFT JOIN s1_users ua ON ua.{-code-18} = u.{-code-18}
WHERE u.{-code-18} > 0
GROUP BY u.id,
u.username,
u.{-code-18};
SQL Fiddle Demo
This will give you:
| USERID | USERNAME | ALLYID | TOTALPOP | TOTALPOINT | TOTALUSERS |
Note that: I couldn't find a column{-code-14}
in thes1_users
table, so I joined the tables1_users
withs1_alidata
with the column{-code-18}
ands1_users
with itself with the same field{-code-18}
. Not sure if this is right or not.
{-code-42}ut there is a big problem in your tables' design. Your tables are .
For instance, it seems that the fields{-code-21}
,{-code-22}
,{-code-23}
and{-code-24}
are related attributes for each user, so you can move them to a new table something like:
{-code-25}
:
{-code-26}
,{-code-27}
a foreign key to the users table,{-code-21}
,{-code-22}
,{-code-23}
,{-code-24}
,- Composite key(
{-code-32}
, userid`).
The same for the fields{-code-33}
,{-code-34}
,{-code-35}
and{-code-36}
, move them to a new table:
{-code-37}
:
{-code-27}
,{-code-33}
,{-code-34}
,{-code-35}
.
However, if these{-code-42}
s are more than three and there are other properties for this{-code-42}
proeprty, you can create a new table{-code-42}s
:
{-code-42}es
:
- {-code-42}ID,
- other properties here ...
Then:
{-code-37}
:
{-code-27}
,{-code-42}ID
,- Composite key(
{-code-49}
,{-code-42}ID
).
Another big issue is with those 38 fields:{-code-51}
, ... ,{-code-51}9
,{-code-51}wait
, ...,{-code-51}9wait
.
You have to have a separate table for those friends like this:
`UsersFriends:
{-code-27}
a foreign key to the users table,{-code-56}
,{-code-57}
a flag 0 or 1.
There for you have only one column friend, in this column you can insert all the friends of these 38 columns like this:
{-code-27} FriendId {-code-57}
1 1 0
1 2 1
1 3 0
...
...
1 19 1
Also try to avoid storing mutiple values as a comma separated string value, like what you did in the column{-code-59}
. Make a new table for it like this:
Users{-code-59}
:
{-code-61}
,{-code-59}Id
.
This was just an example of how you can redesign one of your tableusers
these are just a sample of bad things you have in this table, you have other columns than these that I mentioned. You have also to do the same things for other tables.
For more information, see these:
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.