Add one table's id to another table for specific value mysql [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
| OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | | 3000 |
| 100 | 2009-10-08 00:00:00 | | 1500 |
| 101 | 2009-11-20 00:00:00 | | 1560 |
| 103 | 2008-05-20 00:00:00 | | 2060 |
+-----+---------------------+-------------+--------+
Here i have a two table and i want to add one tables customer id to the another table , please help me on this

You can do it with SQL FOREIGN KEY Constraint on ALTER TABLE. You can see the documentation here.
ALTER TABLE first_table_name
ADD FOREIGN KEY (id)
REFERENCES second_table_name(CUSTOMER_ID)
It should work.

Related

sum and group display in mySql PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a table below.
-----------------------------------------------
id | items | user | department | value | date |
-----------------------------------------
1 | books | john | dept-1 | 5 | 1-1-20
-----------------------------------------
2 | fruits | john | dept-1 | 10 | 3-1-20
-----------------------------------------
3 | books | rehman | dept-2 | 8 | 1-1-20
-----------------------------------------
4 | fruits | rehman | dept-2 | 20 | 9-1-20
-----------------------------------------
5 | woolens | john | dept-1 | 15 | 1-1-20
-----------------------------------------
6 | woolens | vinay | dept-1 | 9 | 3-1-20
-----------------------------------------
7 | veggies | sunil | dept-2| 20 | 10-1-20
-----------------------------------------
now I want to display these items like
department | user | value
--------------------------
dept-1 |
| john | 30
| vinay | 9
dept-2 |
| rehman | 28
| sunil | 20
how can I achieve this in mysql, this will be call in ajax and display in a table by php.
can someone help pls.
I tried doing grouping and used while loop but not able to execute desired output.

Change a row header based on query results without doing multiple queries

I just can't logically think how to do this without doing multiple queries, which I'd like to avoid if possible. I have a mysql table set up like this.
+------+----------+--------------+--------------+
| Id | Opponent | Game Date | Playoffs |
+------+----------+--------------+--------------+
| 1 | Chicago | 2020-08-20 | First Round |
| 2 | Chicago | 2020-08-22 | First Round |
| 3 | Chicago | 2020-08-24 | First Round |
| 4 | Chicago | 2020-08-26 | First Round |
| 5 | Dallas | 2020-08-28 | Second Round |
| 6 | Dallas | 2020-08-30 | Second Round |
| 7 | Dallas | 2020-09-01 | Second Round |
+------+----------+--------------+--------------+
What I want to do is pull the results so I can display them in a table like this.
+------+-------------------+----------------+
| Game | Opponent | Date |
+-------------------------------------------+
| Playoff Round: First Round |
+-------------------------------------------+
| 1 | Chicago | 2020-08-20 |
+------+-------------------+----------------+
| 2 | Chicago | 2020-08-22 |
+------+-------------------+----------------+
| 3 | Chicago | 2020-08-24 |
+------+-------------------+----------------+
| 4 | Chicago | 2020-08-26 |
+-------------------------------------------+
| Playoff Round: Second Round |
+-------------------------------------------+
| 5 | Dallas | 2020-08-28 |
+------+-------------------+----------------+
| 6 | Dallas | 2020-08-30 |
+------+-------------------+----------------+
| 7 | Dallas | 2020-09-01 |
+------+-------------------+----------------+
I just can't quite think how to do it without running separate queries on each round. I'd rather not have to do it that way, I think it's probably unnecessary. Thanks!

PHP Recursive Function base on single id export excel

I have a search page that allow user to key in the member ID and it will list out all the downline that belongs to the user.
I am using easyui treegrid to generate the downline.
Now i need to do an extra button to export out all the downline that belongs to the search id and export each line of information into excel file.
This is part of my data, and actually the real data had more column and about 4000++ of data.
Is there anyone can help me or some references? Please let me know if you need more info
+-------------+---------------+---------------------------+------------+
| MemberID | parent_id | Name | Age |
+-------------+---------------+---------------------------+------------+
| 1 | 0 | Cassy | 8 |
| 2 | 1 | Peter | 7 |
| 3 | 1 | Maide | 7 |
| 4 | 1 | Samda | 7 |
| 5 | 4 | Kinso | 7 |
| 6 | 4 | March | 7 |
| 7 | 2 | Sandy | 10 |
| 8 | 0 | Mandy | 12 |
+-------+---------------+----------------------------------------------+

sql - mysql not able to get desired result for group_concat, group by, transpose

Giving you an example of problem I am facing creating mysql query. using group_concat or group by is not giving me the result I want.
Can anyone please tell me how can i achieve following results? If it can be solved using procedure or views, it is ok.
user
===============
| Id | UserId |
=====+========|
| 1 | 297 |
---------------
workex
=======================================================
| wid | UserId | title | company |
=====+========+===============+=======================|
| 1 | 297 | software engineer | x |
|----+--------+---------------+-----------------------|
| 2 | 297 | sr software engineer | y |
-------------------------------------------------------
education
=========================================================
| eid | UserId | title | institute |
=====+========+===============+=========================|
| 1 | 297 | computer science | p |
|----+--------+---------------+-------------------------|
| 2 | 297 | MS software engineering | q |
---------------------------------------------------------
Result
=====================================================================================================================================================================================
| userid | workex_title | company | workex_title | company | education | institute |education | institute |
=====+========+===============+======================================================================================================================================================|
| 297 | software engineer | x | sr software engineer | y | computer science | p |sr software engineer | q |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Search for time in mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This id my table :
+------------+--------------+---------------------+--------+
| id | playduration | time | id_his |
+------------+--------------+---------------------+--------+
| 129 | 00:00:00 | 2014-01-19 04:08:00 | 1 |
| 190 | NULL | 2014-01-19 02:20:34 | 6 |
| 1390228962 | NULL | 2014-01-19 02:09:20 | 4 |
| 1390228963 | NULL | 2014-01-19 02:06:12 | 3 |
| 188 | NULL | 2014-01-19 02:00:11 | 5 |
| 151 | NULL | 2014-01-19 01:06:01 | 7 |
| 195 | NULL | 2014-01-15 01:10:00 | 2 |
+------------+--------------+---------------------+--------+
and i want to get row id = 1390228963 when I search for time = 2014-01-19 02:07:00
I thought that I have been explained it good
You need to get the last row before the time:
select t.*
from table t
where time <= '2014-01-19 02:07:00'
order by time desc
limit 1;
Just use this mysql query
SELECT * from tablename where time="2014-01-19 02:00:11";

Categories