confusing in mysql database tables - php

i am bit confuse in to getting the result into the two tables, i know it is simple but not getting any clue..
here is my query the gives me the result, but when i am using the group by then it's just giving me the result of one line even the actual result in database is (300, 500) because i am fetching the result through the lead_id which is common in both tables..
SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv, lead_crm_data AS crmd
WHERE inv.lead_id = '310'
AND crmd.lead_id = '310'
GROUP BY inv.lead_id
here is the result of the given query..
amount computers product_id pc_opti
300 2 7, 6 2
300 3 7, 6 3
540 2 7, 6 2
540 3 7, 6 3
result with GROUP BY
300 2 7, 6 2
Desired REsult
300 2 7, 6 2
540 3 7, 6 3

Try this:
SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv, lead_crm_data AS crmd
WHERE inv.lead_id = '310'
AND crmd.lead_id = '310'
GROUP BY inv.amount
You could use a better (IMHO) syntax:
SELECT inv.amount, crmd.computers, crmd.product_id, crmd.pc_opti
FROM invoice AS inv INNER JOIN lead_crm_data AS crmd
ON inv.lead_id = crmd.lead_id
AND inv.lead_id = '310'
GROUP BY inv.amount

Related

How to get data from different tables in MySQL database and display in a table with php

I have read many topics but none have given me exactly what i need
I have 3 tables
products
id
product_name
1
Heineken
2
Budweiser
transaction
id
user_id
date
amount
32
4
01/23/2023
10000
45
2
01/23/2023
20000
57
4
01/23/2023
5000
transaction_details
id
transaction_id
product_id
1
32
1
2
32
2
3
45
2
4
45
1
5
57
1
Now how can i achieve this
SELECT FROM transaction WHERE user_id = 4 then use the results to SELECT FROM transaction_details and display it in a table using a single sql query
And when displaying the results from transaction_details i want to use product_name
This is the result i want
transaction_id
products_id and name
32
1. Heineken
32
2. Budweiser
57
1. Heineken
I have tried to select from transaction table where id is equal to 4 and i got two results, i don't know what to do next
Using Mysqli Join Query with select concat option in in your select query
Please Using This Way
select tran.id,CONCAT(pro.id,'.',pro.product_name) as products_id_and_name from transaction as tran join transaction_details as trand on tran.id = trand.transaction_id JOIN products as pro on pro.id = trand.product_id WHERE tran.user_id = 4;

Mysql tree traverse and count

I`ve got the following table in mysql
id|top_id|amount
1 NULL 2
2 NULL 8
3 NULL 4
4 3 7
5 2 8
6 2 4
7 5 5
8 7 1
9 6 6
10 8 6
For the first 3 ids i need to sum up all amounts of its successors to get the following:
id | amount
1 8
2 32
3 11
I guess there should be joins but unfortunately I can`t get the working mysql request. Can somebody help me?
UPD: In php I have the following query:
$tops = $mysqli->query('SELECT * FROM table WHERE top_id IS NULL')
which obviously returns me just 3 top ids and their plain amounts (2, 8 and 4 respectively). Instead of 2, 8 and 4 I need to get 12, 28 and 11 and that`s the problem((
This is the crudest solution... No doubt someone will provide some hints for a more scalable solution shortly (perhaps employing PHP)...
SELECT a.id
, COALESCE(a.amount,0)
+ COALESCE(b.amount,0)
+ COALESCE(c.amount,0)
+ COALESCE(d.amount,0)
+ COALESCE(e.amount,0)
+ COALESCE(f.amount,0) total
FROM my_table a
LEFT
JOIN my_table b
ON b.top_id = a.id
LEFT
JOIN my_table c
ON c.top_id = b.id
LEFT
JOIN my_table d
ON d.top_id = c.id
LEFT
JOIN my_table e
ON e.top_id = d.id
LEFT
JOIN my_table f
ON f.top_id = e.id
WHERE a.top_id IS NULL;

Sql query to show count(*) of groups while keeping all rows of the group

I have a table similar the following
id user_id father_id
1 1 1
2 2 1
3 3 2
4 4 2
5 5 2
6 6 3
7 7 4
I search for a sql query (prefer Fluent or Eloquent for Laravel 4) to give me the following result:
id user_id father_id family_members
3 3 2 3
4 4 2 3
5 5 2 3
1 1 1 2
2 2 1 2
6 6 3 1
7 7 4 1
As it can be observed family_members is the count of users who have the same father_id
select id, user_id, father_id, count(*) as family_members from users group by father_id
The query above, just keeps the top row of each group, but I want to keep all the other records and not only the first one; then sorting them first according to the family_members and then according to father_id
How can I achieve it?
I donĀ“t know Fluent, nor Eloquent, nor Laravel 4, but the sql query would be like this.
SELECT yourTable.*, auxTable.family_members
FROM yourTable
LEFT JOIN
(
SELECT father_id, COUNT(id) as family_members
FROM yourTable
GROUP BY father_id
)auxTable ON yourTable.father_id = auxTable.father_id
ORDER BY family_members DESC, yourTable.father_id ASC
I solved the problem by the following Fluent query based on the answer from segio0983
$users = DB::table('users')
->leftjoin(DB::raw('(SELECT father_id, COUNT(id) as family_members
FROM users
GROUP BY father_id) auxTable '),function($join)
{
$join->on('users.father_id', '=', 'auxTable.father_id');
})
->orderBy('auxTable.family_members','desc')
->orderBy('users.father_id','asc')
->select('users.id','users.father_id','auxTable.family_members');

Cumulative average mysql php

I have a table name meal
id name carbs protein fat
1 one 10 30 18
2 one 17 4 2
3 one 27 6 7
Now I want to get Cumulative Average of carbs
my query is
SELECT (100*(sum(m.carbs)/(sum(m.carbs)+sum(m.fat)+sum(m.protein))))AS Percantage_carbs FROM `meal` AS m
My query gives result 52.2580645
But I t will be
52.8848076
You Query will result in 44.628099173554 which is absolutely correct.
Total: 10+30+18+17+4+2+27+6+7 = 121
Carbs Only: 10+17+27 = 54
(54 / 121) * 100 = 44,63%
SQL Fiddle: http://sqlfiddle.com/#!2/3d6e5/3
The result of the data given and the query you're using is: 44,628099174
I guess your query should be something like this (your missing the group by):
SELECT (
100 *
(sum(m.carbs)
/
(sum(m.carbs)+ sum(m.fat)+ sum(m.protein)))
) AS Percantage_carbs
FROM `meal` AS m
GROUP BY name

get SUM of another row of GROUP BY'd rows

I have this table:
This selection is is duplicated many times for different var_lines (which pretty much work as one row of data, or respondent for a survey) and set_codes (different survey codes).
With this query:
SELECT
*, COUNT(*) AS total
FROM
`data`
WHERE
`var_name` = 'GND.NEWS.INT'
AND(
`set_code` = 'BAN11A-GND'
OR `set_code` = 'BAN09A-GND'
OR `set_code` = 'ALG11A-GND'
)
AND `country_id` = '5'
GROUP BY
`data_content`,
`set_code`
ORDER BY
`set_code`,
`data_content`
The query basically counts the number of answers for a specific question. Then groups them survey (set_code).
What I need is for each of the grouped data_content answers for GND.NEWS.INT to also show the SUM of all the corresponding GND_WT with the same var_line.
For example if I had this:
data_id data_content var_name var_line
1 2 GND.NEW.INT 1
2 1.4 GND_WT 1
3 2 GND.NEW.INT 2
4 1.6 GND_WT 2
5 3 GND.NEW.INT 3
6 0.6 GND_WT 3
I would get something like this:
data_id data_content var_name var_line total weight
1 2 GND.NEW.INT 1 2 3
5 3 GND.NEW.INT 3 1 0.6
Thanks for any help.
Your requirements are not exactly clear, but I think the following gives you what you want:
select d1.data_id,
d1.data_content,
d1.var_name,
d1.var_line,
t.total,
w.weight
from data d1
inner join
(
select data_content,
count(data_content) Total
from data
group by data_content
) t
on d1.data_content = t.data_content
inner join
(
select var_line,
sum(case when var_name = 'GND_WT' then data_content end) weight
from data
group by var_line
) w
on d1.var_line = w.var_line
where d1.var_name = 'GND.NEW.INT'
See SQL Fiddle with Demo
This Query can be suitable for your specific example:
select st.data_id,
st.data_content,
st.var_name,
st.var_line,
count(st.data_id) as total,
sum(st1.data_content) as weight
from data st
left join data st1 on st1.var_name = 'GND_WT' AND st1.var_line=st.var_line
where st.var_name='GND.NEW.INT'
group by st.data_content
Regards,
Luis.

Categories