Fetching Record Pattern From Database - php

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.

Related

Show rows with a column partially matching a given parameter

Suppose I have a table named testdb which has the following values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
| Alex ghosh | 8985855665 | 4 |
Now, I am performing a search by the name and the input given is just "supriyo" (no last name given).
The search result should show both the values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
Is this possible to achieve using MySQL and php?
You are looking for LIKE:
select *
from your_table
where name like 'supriyo%';
Demo
Edit:
In php, use:
select *
from your_table
where name like '$name%';

multiple or single selection

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

how to merge multiple rows with same id mysql

I'm new posting here but the community have been my best resource on my projects so far.
I'm a dumb/dummy Mysql "wanna be" and I'm in the middle of a project that is making me go mad.
I have a table from wordpress plugin buddypress that pairs meta_key and meta_values in order to create something akin to a taxonomy. My duty is to use these paired values to implement an advanced group search. Here is the original table:
--------------------------------------------
id | group_id | meta_key | meta_value
--------------------------------------------
1 | 1 | time-zone | Kwajalein
2 | 1 | playstyle | hardcore
3 | 1 | recruiting-status | Open
4 | 1 | ilvl | 115
5 | 1 | main-raid | Final Coil of Bahamut
6 | 1 | voicechat | fc.teamspeak3.com
etc....
Using a view I managed to create a more friendly searchable table for begginers :
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 | | |
1 |Kwajalein | |
1 | | hardcore |
1 | | |
1 | | | Final Coil of Bahamut
1 | | |
And here is the view code:
SELECT distinct
group_id AS 'gid',
IF(meta_key='recruiting-status',meta_value,'') AS 'Recruitment',
IF(meta_key='server',meta_value,'') AS 'server',
IF(meta_key='time-zone',meta_value,'') AS 'tzone',
IF(meta_key='main-raid',meta_value,'') AS 'raid',
IF(meta_key='raid-days',meta_value,'') AS 'days',
IF(meta_key='playstyle',meta_value,'') AS 'playstyle',
IF(meta_key='raid-progression',meta_value,'') AS 'progression',
IF(meta_key='raid-time',meta_value,'') AS 'time',
IF(meta_key='tanker-spot',meta_value,'') AS 'tank',
IF(meta_key='healer-spot',meta_value,'') AS 'healer',
IF(meta_key='melee-dps-spot',meta_value,'') AS 'melee',
IF(meta_key='ranged-dps-spot',meta_value,'') AS 'ranged',
IF(meta_key='magic-dps-spot',meta_value,'') AS 'magic',
IF(meta_key='ilvl',meta_value,'') AS 'ilvl',
IF(meta_key='voicechat',meta_value,'') AS 'voice',
IF(meta_key='voicechatpass',meta_value,'') AS 'voicep',
FROM wpstatic_bp_groups_groupmeta
The point is, I need to merge that result (view) so all the group_id=1 or 2 or 3, etc stand in one single row, like this:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 |Kwajalein | hardcore | Final Coil of Bahamut
2 |SaoPaulo | regular | Second Coil of Bahamut
etc
Can anyone help me there?
Just surround your IFs in a MAX, or another aggregate function that will capture the non-empty strings (e.g., GROUP_CONCAT), and add a GROUP BY group_id add the end. For example,
SELECT
group_id AS gid,
MAX(IF(meta_key='recruiting-status',meta_value,'')) AS 'Recruitment',
MAX(IF(meta_key='server',meta_value,'')) AS 'server',
...
FROM wpstatic_bp_groups_groupmeta
GROUP BY group_id

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.)

Get all dbpprops from a certain box in wikipedia

Let me use these 2 links as my examples:
http://en.wikipedia.org/wiki/Maarten_Stekelenburg
http://dbpedia.org/page/Maarten_Stekelenburg
What I want to do is get all the dbpprops which are in the top right infobox (the one with the picture).
Here I encounter 2 problems:
How do I get all the properties without knowing them (We are putting together a football player database, and the prop names are all different for every player)
How do I make sure those are the properties that are inside the box, because there are many other dbpprops on that page and it doesn't look like dbpedia differentiates between those.
After that I want to add all those properties to an array(if that is possible).
If it's possible, I would like it to look something like this:
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX dbp2: <http://dbpedia.org/ontology/>
PREFIX dbp3: <http://dbpedia.org/property/>
SELECT *
WHERE {
dbp:".$term." dbp2:abstract ?abstract .
dbp:".$term." dbp2:thumbnail ?img .
dbp:".$term." dbp3:* ?properties . //This would then be replaced by the necessary line
FILTER langMatches(lang(?abstract), 'en')
}
EDIT: I am working with PHP
I can't guarantee that the set of properties from the infobox will be exactly the properties that are dbpprop properties (as opposed to dbpedia-owl properties), but it looks like there's a fairly good correspondence. In this case, you could use a query like the following which asks for properties and values of the person you mentioned, but only thoe properties that are in the dbpprop namespace.
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpprop: <http://dbpedia.org/property/>
select ?property ?value where {
dbpedia:Maarten_Stekelenburg ?property ?value
filter( strstarts(str(?property),str(dbpprop:)) )
}
SPARQL results
----------------------------------------------------------------------------------------------------------------------
| property | value |
======================================================================================================================
| dbpprop:bg | "gold"#en |
| dbpprop:bg | "#F1771D"#en |
| dbpprop:birthDate | "1982-09-21+02:00"^^<http://www.w3.org/2001/XMLSchema#date> |
| dbpprop:birthPlace | "Haarlem, Netherlands"#en |
| dbpprop:caps | 44 |
| dbpprop:caps | 191 |
| dbpprop:clubnumber | 24 |
| dbpprop:clubs | dbpedia:A.S._Roma |
| dbpprop:clubs | dbpedia:AFC_Ajax |
| dbpprop:currentclub | dbpedia:A.S._Roma |
| dbpprop:dateOfBirth | 22 |
| dbpprop:fg | "navy"#en |
| dbpprop:fg | "white"#en |
| dbpprop:fullname | "Maarten Stekelenburg"#en |
| dbpprop:goals | 0 |
| dbpprop:name | "Maarten Stekelenburg"#en |
| dbpprop:name | "Stekelenburg, Maarten"#en |
| dbpprop:nationalcaps | 4 |
| dbpprop:nationalcaps | 54 |
| dbpprop:nationalgoals | 0 |
| dbpprop:nationalteam | dbpedia:Netherlands_national_football_team |
| dbpprop:nationalteam | dbpedia:Netherlands_national_under-21_football_team |
| dbpprop:nationalyears | 2002 |
| dbpprop:nationalyears | 2004 |
| dbpprop:ntupdate | 18 |
| dbpprop:pcupdate | 21 |
| dbpprop:placeOfBirth | "Haarlem, Netherlands"#en |
| dbpprop:position | <http://dbpedia.org/resource/Goalkeeper_(association_football)> |
| dbpprop:shortDescription | "Dutch footballer"#en |
| dbpprop:title | "Awards"#en |
| dbpprop:title | "Netherlands squads"#en |
| dbpprop:years | 2002 |
| dbpprop:years | 2011 |
| dbpprop:youthclubs | dbpedia:AFC_Ajax |
| dbpprop:youthclubs | "Schoten"#en |
| dbpprop:youthclubs | "Zandvoort '75"#en |
| dbpprop:youthyears | 1997 |
| dbpprop:wordnet_type | <http://www.w3.org/2006/03/wn/wn20/instances/synset-soccer_player-noun-1> |
| dbpprop:hasPhotoCollection | <http://wifo5-03.informatik.uni-mannheim.de/flickrwrappr/photos/Maarten_Stekelenburg> |
----------------------------------------------------------------------------------------------------------------------
You mentioned storing results in an array, but I don't really understand the example that you've provided. It looks more like a query than code to put something into an array. Additionally, you haven't specified what programming language you're working with, so we can't really respond with array manipulation code.

Categories