multiple or single selection - php

i have two tables and column name are as :
Table 1
user | food | color | bike | car
Table 2
user | mobile | laptop
Now i want to get result by select single or multiple value.
For example, if i want select user which have bike and laptop . then i can get result it by query but for this all fields i have to use many condition . i have used if else where. and i also want to refine select with current selection . so what should i use ? Please help my previous question was same but i did not asked perfectly. so asked again. Thank You.

You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.
Example
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

Related

Dynamically create MySQL table columns

I have the following MySQL table which is structured like that:
| id | bonus0 |
Now I want to add the following data set:
| id | bonus0 | bonus1 | bonus2 | bonus3 |
| 10 | 4582 | 2552 | 8945 | 7564 |
As you can see the columns bonus1 - bonus3 aren´t created yet.
How would a php script/ query look like which checks if enough columns are already available and if not which will create the missing ones with consecutive numbers at the end of the word "bonus"?
So in the example the columns bonus1 - bonus3 would be created automatically by the script.
In reality (I mean a normalized relational database) you should have 3 tables. Lets call them people, bonuses and bonus_to_person
people looks like:
+-----------------+------------+
| person_id | name |
+_________________+____________+
| 1 | john |
+-----------------+------------+
| 2 | frank |
+-----------------+------------+
bonuses Looks like
+----------------+--------------+
| bonus_id | amount |
+________________+______________+
| 1 | 1000 |
+----------------+--------------+
| 2 | 1150 |
+----------------+--------------+
| 3 | 1200 |
+----------------+--------------+
| 4 | 900 |
+----------------+--------------+
| 5 | 150 |
+----------------+--------------+
| 6 | 200 |
+----------------+--------------+
bonus_to_person Looks like
+----------------+-----------------+
| bonus_id | person_id |
+________________+_________________+
| 1 | 1 |
+----------------+-----------------+
| 2 | 2 |
+----------------+-----------------+
| 3 | 2 |
+----------------+-----------------+
| 4 | 1 |
+----------------+-----------------+
| 5 | 1 |
+----------------+-----------------+
| 6 | 1 |
+----------------+-----------------+
This way, any ONE person can have unlimited bonuses simply by INSERTING into bonuses with the amount, and INSERTING into bonus_to_person with the bonus_id and person_id
The retrieval of this data would look like
SELECT a.name, c.amount from people a
LEFT JOIN bonus_to_people b
ON a.person_id = b.person_id
LEFT JOIN bonuses c
ON c.bonus_id = b.bonus_id
WHERE a.person.id = 1;
Your result from something like this would look like
+------------+----+-------+
| name | amount |
+____________+____________+
| john | 1000 |
+------------+------------+
| john | 900 |
+------------+------------+
| john | 150 |
+------------+------------+
| john | 200 |
+------------+------------+
You should be using this normalized approach for any database that will continue growing -- Growing "deeper" than "wider" is better in your case ..
// Get existing columns of the table
// $queryResult = run SQL query using PDO/mysqli/your favorite thing: SHOW COLUMNS FROM `table`
// Specify wanted columns
$search = ['bonus0', 'bonus1', 'bonus2', 'bonus3'];
// Get just the field names from the resultset
$fields = array_column($queryResult, 'Field');
// Find what's missing
$missing = array_diff($search, $fields);
// Add missing columns to the table
foreach ($missing as $field) {
// Run SQL query: ALTER TABLE `table` ADD COLUMN $field INT
}

Fetching Record Pattern From Database

I have a table like this in my database.
| id | yes_answer | no_answer |
| a01 | a05 | a04 |
| a03 | a16 | a32 |
| a04 | c01 | a05 |
| a05 | a06 | c06 |
| a06 | a08 | a09 |
| a08 | c03 | a09 |
| a09 | a03 | |
| a16 | a33 | a02 |
| a32 | c04 | a16 |
| a33 | c05 | |
As An Explanation :
1.id column represent an id of question. it is linked to another table on my database.
2.yes_answer column represent the next question (like a01) or conclusion (like c04) you get when you answer the question represented by id column with yes or TRUE or 1 value.
3.no_answer is generally the same as yes_answer column except that you get it when you answer question represented by id column with no, FALSE, 0 value.
4. Every id, yes_answer, no_answer is connected each other. The Result is A Pattern. When you Want to find a path that lead to answer c01 it will be like c05 -> a33,a16,a32,a03,a09,a06,a05,a01.
And Here I am Trying To Bactrack The Pattern From answer column that marked with c like c01 or c02 like above. And I've been able to bactrack only the beginning value of it Like (c05 -> a33) and the rest is blank.
Here is My Code
$sqlQueryYes="SELECT * FROM tbl_path_inference WHERE yes_answer='c05'";
$execQueryYes=mysql_query($sqlQueryYes);<br />
$queryStatus=mysql_num_rows($execQueyYes); //Getting Query Status<br />
if($queryStatus == 1) {
$getDataYes=mysql_fetch_array($execQueryYes);
$ansPath=$ansPath." ".$getDataYes['id'];
} else { // if cant find answer on column yes_answer try on no_answer
$sqlQueryNo="SELECT * FROM tbl_path_inference WHERE no_answer='c05'";
$execQueryNo=mysql_query($sqlQueryNo);
$getDataNo=mysql_fetch_array($execQueryNo);
$ansPath=$ansPath." ".$getDataNo;
}
Anyone Have Solution On It. Im Still working It Now.

How to count number of rows with the same column data and display to table?

I have 2 tables, the 'department' and 'document'.
Table department
| doc_id | dept_name |
----------------------------------
| 1 | Information Technology|
| 2 | Software Development |
| 3 | Human Resource |
| 4 | Accounting |
| 5 | Support |
Table document
| doc_id | doc_name | author | description | department |
----------------------------------------------------------------------------
| 1 | Maps | User1 | sample | Information Technology |
| 2 | Audits | User3 | sample | Software Development |
| 3 | Image | User1 | sample | Information Technology |
| 4 | Papers | User4 | sample | Human Resource |
| 5 | Print Screen| User1 | sample | Software Development |
| 6 | Transaction | User3 | sample | Accounting |
| 7 | Graph | User1 | sample | Support |
| 8 | Excel | User1 | sample | Information Technology |
Now, I want to display the table with two columns: department and total_doc.
Output:
| department |total_doc|
-----------------------------------
| Information Technology| 3 |
| Software Development | 2 |
| Human Resource | 1 |
| Accounting | 1 |
| Support | 1 |
I want to display the total document inside the department and arrange them in ascending order.
Here's my query.(not sure)
SELECT department, count(doc_name) as 'total_doc' FROM tbl_document GROUP BY doc_name
I'm using MVC pattern in Codeigniter.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('doc_name');
Also, How can I display this in table? like using foreach in html?
You need to do group by with department not with doc_name.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('department');
$result = $this->db->get()->result();
Hope This will help you.
foreach ($result as $row)
{
echo $row->department."----".$row->total_doc;
}
here you go
SELECT dept_name,COUNT(td.department) FROM department d
LEFT JOIN tdocument td ON td.`department`=d.`dept_name`
GROUP BY td.`department` ORDER BY COUNT(td.`department`) DESC;
You want one line per department. IN SQL words: You want to group by department.
select department, count(*) as total_doc from document group by department;
(BTW: don't use single quotes for column aliases.)

query specific table based on search keyword in mysql

I am creating a search portal in PHP from which user can search for a specific cuisine. In MySQL I have multiple tables for each cuisine and the respective hotel names that offer the cuisine. For example, in table
How can I query a specific cuisine table based on the cuisine search keyword?
So if a user enters 'mexican' as the search query, how can it connect to the 'Table2 - Mexican' and return the hotel names from this table?
Table1 - Chinese
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Mexican
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table3 - Pizza
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Your database concept is very unflexible. I think you should put the cuisines into your database as information (i.e. table content) instead of metadata describing single tables. Tables should generally considered to be static just like the code you write to access the database and its tables. If you implement the cuisines as different tables you would have to hardwire every cuisine into your code.
Here is a suggestion for a better approach:
Create a hotels table to store all the hotels,
Create a cuisines table to store all the different types of cuisines,
Make an additional table to establish the n:m relationship between the hotel and the cuisine.
Example:
hotels: id, name, address, city, telno, email
cuisine: id, name, description
rel: cuisine, hotel (where both are the foreign keys to the
id columns of the respective tables above)
See also:
How to handle a Many-to-Many relationship with PHP and MySQL.
MySQL: Many To Many Relationships » Return True
You might want to check this question to create a many-to-many relationship:
many-to-many and many-to-many intersections
I guess what you would like to achieve is something like this:
Table1 - Hotel
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Cuisine
____________________________________________
| id | cuisine_name | keywords |
|______|______________|____________________|
| 1 | Chinese | Shandong,Noodles,. |
| 2 | Mexican | Tacos,Beans,... |
| 3 | Itarian | Pizza,Pasta,.. |
|______|______________|____________________|
Table3 - HotelCuisine
___________________________________
| id | hotel_id | cuisine_id |
|______|____________|______________
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 3 |
|______|____________|_____________|
SQL:
SELECT hotelname, cuisine_name FROM Hotel
INNER JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id
INNER JOIN Cuisine ON Cuisine.id = HotelCuisine.cuisine_id
WHERE keywords like '%pizza%'
Result:
________________________________________
| hotelname | cuisine_name |
|_______________|______________________|
| hotel1 | Itarian |
| hotel3 | Itarian |
|_______________|______________________|
DEMO: http://sqlfiddle.com/#!2/961de/1
Hope this helps
you can check SQL UNION. But instead of having multiple tables with the same fields, you can try normalization to minimize the redundancy and to make queries easier.
Something like:
Hotel Table
-----------------------------
id | hotelname | categoryID
------------------------------
1 | hotel name 1 | 1
2 | hotel name 2 | 2
-----------------------------
Category Table
-------------------
id | categoryname
-------------------
1 | chinese
2 | mexican
------------------
And query as simple as:
SELECT a.hotelname, b,categoryname
FROM hotel_table a
LEFT JOIN category_table b
ON a.categoryID = b.id AND b.categoryname LIKE '%mexican%';

display details when i click "show"

hello can somebody give me idea how to do this problem? let say for example i have two table "sales" and "rsales" . and i have this ff data for my tables.
data from sales
receipt | date | total | 1st_id | last_id |
129999 | 09/26/2013 | 1220 | 1 | 2 |
139999 | 09/27/2013 | 2320 | 3 | 4 |
data from rsales
id | product_name |
1 | 33uf |
2 | 44uf |
3 | 7sss |
4 | 8sss |
and this is my view from my web page when do reports
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so here it is. my problem is that when i click "+" it will select data from rsales and then display. so let say i cllick "+" where receipt is 129999 . so the expected output must be something like this.
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
id | product_name |
1 | 33uf |
2 | 44uf |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so why it display like this? it is because 1st_id and last_id from sales = id 1 and 2 from rsales. it is hard to explain but i think the example i gave is enough to understand what i want to do. hoping your idea. i really need idea.
if you have it set up the way I think you have it set up you'll need two queries:
SELECT 1st_id, last_id FROM sales WHERE receipt=?
and then
SELECT Id, product_name FROM rsales WHERE Id>=[variable from 1st_id] AND Id<=[variable from last_id];
if I were doing it I would have set up 3 tables
a sales_rsales with 3 fields, id, sales_receipt, rsales_id.Get rid of 1st_id and last_id use that to track the products per receipt then the SQL will be easy:
SELECT rsales.Id, rsales.product
FROM rsales JOIN sales_rsales ON rsales.Id=sales_rsales.rsales_id
JOIN sales ON sales.receipt=sales_rsales.sales_receipt
WHERE sales.receipt=?

Categories