简体   繁体   中英

Count SQL query PHP

I want to count for each gallery number of images in it, here's my query:

$req = db_query("SELECT count(*) FROM node,node_file WHERE node.nid = node_file.nid AND nid = ".$value['nid']);

here's the two tables :
node_file:

nid  |fid   |
-------------
  1  |  1   | 
-------------
  1  |  2   |  
-------------
  1  |  3   |
-------------
  2  |  1   |
-------------
  2  |  2   |
-------------
  2  |  3   |

node:

 nid |type  |
-------------
  1  |Gallery1| 
-------------
  2  |gallery2|  

The error : Query : SELECT count(*) FROM node,node_file WHERE node.nid = node_file.nid AND nid = 34 Message : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'nid' in where clause is ambiguous

Thanks!

Both of your tables have column nid and you are using column in AND clause so you need to specify to which table it belongs.

SELECT 
    count(*)
FROM
    node,
    node_file
WHERE
    node.nid = node_file.nid AND node.nid = 34

OR

SELECT 
    count(*)
FROM
    node,
    node_file
WHERE
    node.nid = node_file.nid AND node_file.nid = 34

用这个

$req = db_query("SELECT count(*) FROM node,node_file WHERE node.nid = node_file.nid AND node.nid = ".$value['nid']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM