php mysql how to select what i need from 2 tables [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 7 years ago.
Improve this question
how can i select data from 2 columns
i have table countrys:
country_id | country
--------------------
1 | England
2 | Poland
3 | Italy
and more...
and i have table Messages:
message_id | message | country_id
---------------------------------
1 | text.. | 1
2 | text.. | 2
3 | text.. | 1
1 | text.. | 3
and i need result like this:
message | country
--------------------
text.. | England
text.. | Poland
text.. | England
text.. | Italy
Query example please for my understand!

Try something like this as your select statement
SELECT message, country FROM Countrys AS c
INNER JOIN Messages as m on c.country_id = m.country_id;

Use Join
Select message,country From Messages m join countrys c
on m.country_id=c.country_id

Related

Get data from more then 2 table against 1 id in php sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have four different table. And invoice_id is foreign key in other table.
i want to show all records against invoice_id.
invoice_id | Bill_amount | Commission_amount | Payment_amount |
| | | |
2 | ------ | 1000 | 500 |
2 | ------ | 200 | 100 |
2 | ------ | -------------- | 100 |
2 | ------ | ------------ | 50 |
3 | 100 | 200 | -------- |
And So On........
Suppose you need 4 columns from 4 different tables. You can use left join if you want to include the empty records also from table2, table3 and table4.
Try the following query:
SELECT
table1.invoice_id,
table2.Bill_amount,
table3.Commission_amount,
table4.Payment_amount
FROM
table1
LEFT JOIN table2 ON table1.invoice_id = table2.invoice_id
LEFT JOIN table3 ON table1.invoice_id = table3.invoice_id
LEFT JOIN table4 ON table1.invoice_id = table4.invoice_id
WHERE
table1.invoice_id = '(your_required_id)';

Query for showing the Passers from the database [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 years ago.
Improve this question
Student_ID | First_Name | Last_Name | Examination_date | Exam_grade
| | | |
12345 | John | Doe | 12/01/2019 | 5
67890 | Johny | Bravo | 12/02/2019 | 7
09876 | Johnny | Boy | 12/02/2019 | 3
Hello Good day, I just want to show only the students who passed the exam, that has a minimum grade of 5 above. Thank you so much
here's my query
$conn->query("SELECT * FROM tbl_admission where exam_grade > 5");
use this condition - where exam_grade >= 5
SELECT * FROM tbl_admission where exam_grade >= 5
For having grade 5 or above you should use >= in your query .
SELECT * FROM tbl_admission where exam_grade >= 5

Fetch many to many relationship table data [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 4 years ago.
Improve this question
I was trying to fetch data from following tables.
table_question
q_id | question
1 | q1
2 | q2
3 | q3
4 | q4
table_answer
a_id| answer
1 | a1
2 | a2
3 | a3
4 | a4
5 | a5
table_entity
e_id| q_id | a_id
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 4
5 | 3 | 5
6 | 4 | 2
I want a question and answer. Please give me some suggestion.
Fetche data by using doctorine 2 ORM.
Try this mysql with your phpmyadmin
$qry = "SELECT quetion.que,answer.ans FROM entity INNER JOIN quetion ON quetion.id=entity.q_id Inner Join answer On answer.id=entity.a_id";
There are use join table for join those table and get the data from those table
you need to use join query. With joining both table you can get all tables relational data
$query = SELECT table_question.question,table_answer.answer FROM table_entity INNER JOIN table_question ON table_question.id=table_entity.`q_id ` Inner Join table_answer On table_answer.id=table_entity.a_id ;
Here you go:
SELECT table_question.question, table_answer,answer FROM table_entity
JOIN table_question ON (table_entity.q_id = table_question.q_id)
JOIN table_answer ON (table_entity.a_id = table_answer.a_id)

Sum top three totals and group result by [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my problem...
table looks like this:
|id |coutry |sum
| 0 | USA | 30
| 1 | USA | 60
| 2 | USA | 90
| 3 | USA | 80
| 4 | GER | 40
| 5 | GER | 90
| 6 | SWE | 30
| 7 | SWE | 10
| 8 | SWE | 90
| 9 | SWE | 40
and result (top three scores if exists) should be like this:
|coutry |total
| USA | 230
| GER | 130
| SWE | 160
Any change to solve this by sql only? I'll fetch final result using php foreach..
The limit keyword should do the trick:
SELECT country, SUM(`sum`)
FROM my_table
GROUP BY country
ORDER BY 2 DESC
LIMIT 3
You need to establish a row number to get the top 3 sums per country. To do this with MySql, you have to use user-defined variables.
SELECT country, SUM(`sum`)
FROM (
SELECT *,
#rn:=IF(#prevCountry=country,#rn+1,1) rn,
#prevCountry:=country
FROM yourtable, (SELECT #rn:=0, #prevCountry:='') t
ORDER BY country, `sum` DESC
) t
WHERE rn <= 3
GROUP BY country
ORDER BY country
SQL Fiddle Demo
Try it here because #Mureinik have right.
SQL Fiddle
Order by country DESC solve "TOP" problem.

how to use multi-keywords search with php and 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
how to use multi-keywords search with php and mysql ?
I have a product table like this
the keywords field is save the keyword id
| id | name | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1 |
and the keyword table like this
|id | name |
|1 | white |
|2 | eat |
I want use a product keywords field find the similar product, how can I do it?
If you're set on using this data structure, you can use FIND_IN_SET
SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)
What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:
product_has_keyword
product_id | keyword_id
------------------------
112 | 123
112 | 12
112 | 421
112 | 121
That way you can use index for a join (which will be much faster)

Categories