php


Query from comment table which is related to itself?


I have a comment table which is related to itself with id as PK and parent as FK. It's one level hierarchical storage data somehow. Each comment can have just one parent which doesn't have parent or parent=NULL. It means just root comment can have a reply.
This is my table fields:
id user_id post_id comment parent username
For simplicity I store username in this table and we don't need to join user_id with user table.
I want show each comment and a link to the parent comment if it has parent, it's important show last inserted comment first.
I try this query which is not giving me what I want:
SELECT COMMENT . * , c1 . *
FROM `comment`
LEFT JOIN COMMENT c1 ON c1.parent = comment.id
if id is an autoincrement try somth like this
SELECT COMMENT . * , c1 . field1 AS parent_field1, c2.field2 AS parent_field2 /*and so on*/
FROM `comment`
LEFT JOIN COMMENT c1 ON c1.parent = comment.id
ORDER BY id DESC

Related Links

SEO Friendly URL like Magento
PHP generate jQuery data
JSON_DECODE() not working?
insert more than one array using one query
PHP header() usage and why it isn't redirecting
If 0 items in DB table, return error
Codeigniter pass data variables from controller to view
IBM-DB2 installtion failed on xampp1.7.1 on linux system
Undefined index: template_id
PHP PDO sqlite: Unable to open database
PHPExcel: Exception “Could not close zip file” on second created file
Passing a variable through post
.htaccess to redirect from root dir to PHP script
Video Host Broken link check
Still see errors: php.ini display errors line is being ignored
preg_match: and-not expression

Categories

HOME
angular-ui-grid
android-fragments
bokeh
concurrency
postgresql-9.4
operating-system
nsview
whmcs
nsis
implicit-conversion
kurento
yahoo
driver
migrate
mailgun
pickle
pe
weex
fido-u2f
animated-gif
spreadsheetgear
points
tortoisehg
phpstorm-2016.1
memory-fences
reporting
advanced-filter
autoencoder
pdfminer
jade4j
aurelia-cli
ms-dos
ejml
viewcontroller
access-denied
linq-to-excel
gcal
entropy
embedded-v8
ptvs
dtd
apache-modules
mechanicalturk
business-rules
pbx
xcode6.4
thread-exceptions
decoder
fuzzer
nunit-console
wolfram-language
uibinder
jeasyui
mnesia
currency-exchange-rates
jsr363
django-validation
phpquery
smart-tv
fanotify
dnx50
blank-line
gluon-desktop
css-paged-media
unhandled
cache-manifest
console.log
text-align
python-curses
sablecc
dos2unix
xhprof
jini
typesafe-stack
django-sites
cloo
sql-view
insertonsubmit
stty
quickgraph
insertion
xs
unicoins
significant-digits
ckeditor.net
idispatch
struts2-json-plugin
thejit
jzmq
liveconnect
fitch-proofs
haiku
grails-validation
3des
bll
libxslt
hungarian-notation
main-method

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile